项目结构图 (Spring3.0.2 +mybatis3.0.4)
方案一: 通过配置文件整合Spring和mybatis
应用数据库
-- --数据库 tb_user -- drop table if exists tb_user; create table tb_user( id int primary key auto_increment comment '主键', username varchar(40) not null unique comment '用户名', password varchar(40) not null comment '密码', email varchar(40) comment '邮件', age int comment '年龄', sex char(2) not null comment '性别' ); |
对应的实体类
package com.icreate.entity; /** * * * @version : 1.0 * * @author : 苏若年 * * @since : 1.0 创建时间: 2013-4-9 上午11:15:50 * * @function: TODO * */ public class User { private int id; private String username; private String password; private String sex; private String email; private int age; //getter() and setter () } |
数据Dao接口
package com.icreate.dao; import java.util.List; import com.icreate.entity.User; /** * * * @version : 1.0 * * @author : 苏若年 * * @since : 1.0 创建时间: 2013-4-9 上午11:36:34 * * @function: TODO * */ public interface UserDao { public int insert(User user); public int update(User user); public int delete(String userName); public List selectAll(); public int countAll(); public User findByUserName(String userName); } |
在config目录下进行Mapper文件配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.icreate.dao.UserDao"> <select id="countAll" resultType="int"> <!-- 查询表中记录总数 --> select count(*) c from tb_user; </select> <select id="selectAll" resultType="com.icreate.entity.User"> <!-- 查询表中的所有用户 --> select * from tb_user order by username asc </select> <insert id="insert" parameterType="com.icreate.entity.User"> <!-- 向数据库中插入用户 --> insert into tb_user(username,password,email,sex,age) values(#{username},#{password},#{email},#{sex},#{age}) </insert> <update id="update" parameterType="com.icreate.entity.User"> <!-- 更新库中的用户 --> update tb_user set username=#{username},password=#{password},email=#{email},sex=#{sex},age=#{age} where username=#{username} </update> <delete id="delete" parameterType="String"> <!-- 删除用户 --> delete from tb_user where username=#{username} </delete> <select id="findByUserName" parameterType="String" resultType="com.icreate.entity.User"> <!-- 根据用户名查找用户 --> select * from tb_user where username=#{username} </select> </mapper> |
Mybatis应用配置文件MyBatis-Configuration.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="com/icreate/dao/UserDao.xml"/> </mappers> </configuration> |
Spring配置文件,本例中我们放在/WebRoot/WEB-INF/applicationContext.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis?useUnicode= true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> </beans> |
同目录下的web.xml文件进行如下配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> |
配置说明:
<context-param/> 节点配置contextConfigLocation属性是为了让ContextLoaderListener找到Spring上下文的位置并加载它,如果不指定contextConfigLocation,ContextLoaderListener会到/WEB-INF/目录下找applicationContext.xml来加载。
<listener/> 节点上配了ContextLoaderListener。它实现了ServletContextListener接口,所以web server启动时ContextLoaderListener能读取到ServletContext,也就能通过<context-param/>指定的Spring上下文的路径来找到上下文并加载它。
当然,如果你不需要Spring来管理你的Bean,可以去掉上面两个节点。
定义service接口
package com.icreate.service; import java.util.List; import com.icreate.entity.User; /** |
实现service接口,执行dao操作
package com.icreate.service.impl; import java.util.List; import com.icreate.dao.UserDao; /** private UserDao userDao; public void setUserDao(UserDao userDao) { public int countAll() { public int delete(String userName) { public User findByUserName(String userName) { public int insert(User user) { public List<User> selectAll() { public int update(User user) { } |
接下来写我们的测试用例
package com.icreate.service; import java.util.List; import org.junit.Before; import com.icreate.entity.User;
"\t密码:" + user.getPassword() + "\t邮箱:" + user.getEmail()); } |
方案二:使用注解整合
这次将配置文件放置在config目录下.
数据库表与实体类同上
数据dao接口定义
package com.icreate.dao; import java.util.List; import org.apache.ibatis.annotations.Delete; import com.icreate.entity.User; /** @Insert("insert into tb_user(username,password,email,sex,age) values(#{username},#{password},#{email},#{sex},#{age})") |
Service接口与实现不变.
Spring配置文件内容如下
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis?us eUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean"> </beans> |
Web.xml文件配置如下
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <!-- 配置上下文监听 --> <welcome-file-list> |
测试类大致上没有变化,只是ApplicationContext初始化的方法改变成了下面方式
ApplicationContext context = null; UserService userService = null; @Before public void initContext(){ this.context = new ClassPathXmlApplicationContext("applicationContext.xml"); //this.context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"); this.userService = (UserService) context.getBean("userService"); } |
至此我们Spring与mybatis整合实例应用演示完毕
热爱生活,热爱Coding,敢于挑战,用于探索 ...