- 性别
- 保密
- 积分
- 100
- 积分
- 351
- 精华
- 6
- 阅读权限
- 100
- 注册时间
- 2013-5-16
- 最后登录
- 2013-11-8
- 帖子
- 43
- 性别
- 保密
|
作者:liugenhua 日期:2013-10-19 原文地址:http://www.skywj.com/thread-9233-1-1.html
之前在csdn发布了mybatis的入门学习帖---http://bbs.csdn.net/topics/390605626
开发工具:eclipse 所需jar包 mysql-connector-java-5.1.7-bin.jar(mysql连接jar包) mybatis-3.2.3-SNAPSHOT.jar(mybatis核心jar包)mybatis-spring-1.1.1.jar(和Spring整合必须的) 其他jar包(主要是Springweb框架jar包 官网有下载)
同样以一个实体类为例讲述---
简单用个实体类吧--
public class Student(){
private String sno;//学号
private Stirng sname;//姓名
private String sex;//性别
/************相关get,set方法省略********/
}
这是基于注解spring配置
<!-- Spring 配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/ITschool-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
ITschool-servlet.xml,这个文件名字定义 内容如下:-->
<?xml version="1.0" encoding="UTF-8"?>
<!-- ****************************************************
******
******************************************************** -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="org.ITschool">
<context:include-filter type="regex" expression=".*.controller"/>
</context:component-scan>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/Views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
dao接口类实现细节(就以插入记录为例):
public interface StudentDao{
@Insert("insert into student(sno,sname,sex) values(#{sno},#{sname},#{sex})")
public boolean addStudentInfos(Student student);
}
Spring 核心参数文件(SpringIt.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 解析数据资源文件的配置 -->
<bean id="propertyConfigure"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpathataSource.properties</value>
</list>
</property>
</bean>
<!-- 创建数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="maxActive" value="${maxActive}" />
<property name="maxIdle" value="${maxIdle}" />
</bean>
<!-- 创建session工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- daobean的配置 -->
<bean id="StudentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="自己定义的Studentdao接口的package路径(包名)" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
最关键的一步了 就是调用了(实现数据的插入)客户端代码
context=new ClassPathXmlApplicationContext("SpringIt.xml");(这个SpringIt.xml在src根路径下,内容就是上面的配置)
StudentDao studao=(StudentDao)context.getBean("StudentDao");
//实现数据的插入。。。。
Student stu=new Student();
stu.setSno("123");
stu.setSname("张三");
stu.setSex("男");
studao.addStudentInfos(stu);
//OK执行完成!数据插入到数据库、
/*************优势:我们只需写sql语句具体实现交给mybatis框架完成***********************/
有问题留言 我们共同讨论....
|
|