1.pom.xml文件增加mybatis3的jar包如下,本人用的spring版本为4.1.6.RELEASE
!-- 添加mybatis的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- 添加mybatis与Spring整合的核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency>
2.配置文件目录结构,有多少个业务在mapping下就有多少个配置,相应在dao下就有多少个映射类
在*ServiceImpl使用的时候根据需要@autowire不同的Mapper类即可,如下,
3.*ServiceImpl.java--T_USERServiceImpl.java
package com.zhxjz.service.t_user.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.zhxjz.dao.t_user.T_USERMapper; import com.zhxjz.model.t_user.T_USER; import com.zhxjz.service.t_user.T_USERService; @Service("t_userService") @Transactional public class T_USERServiceImpl implements T_USERService { @Autowired private T_USERMapper t_userMapper; @Override public void addUser(T_USER user) { t_userMapper.insert(user); T_USER user1 = new T_USER(); user1.setUserId(user.getUserId()); user1.setUserName(user.getUserName()); user1.setUserSalary(user.getUserSalary()); user1.setUserBirthday(user1.getUserBirthday()); // t_userMapper.insert(user1);//这里是为了测试事务失败回滚而设计的 } @Override public T_USER getUserById(String userId) { return t_userMapper.selectByPrimaryKey(userId); } }
关于事务处理:跟hibernate4一样,只需要在SpringDatasource.xml文件配置事务,spring能自动代理管理事务,发生事务失败则不会commit
4.*Dao.java--T_USERMapper.java(注:mybatis由于接口映射sql的机制,因此*DaoImpl已经不用写了,或者说已被mybatis框架实现了)
package com.zhxjz.dao.t_user; import com.zhxjz.model.t_user.T_USER; public interface T_USERMapper { int deleteByPrimaryKey(String userId); int insert(T_USER record); int insertSelective(T_USER record); T_USER selectByPrimaryKey(String userId); int updateByPrimaryKeySelective(T_USER record); int updateByPrimaryKey(T_USER record); }
5.model类--T_USER.java
package com.zhxjz.model.t_user; import java.util.Date; public class T_USER { private String userId; private String userName; private Date userBirthday; private Double userSalary; 省略getter和setter。。。 }
6.SpringMybatis3.xml--spring管理mybatis核心配置
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapping/*.xml" /> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 --> <property name="basePackage" value="com.zhxjz.dao.t_user;" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
7.T_UserMapper.xml(sql-mapping映射文件配置)
<?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.zhxjz.dao.t_user.T_USERMapper"> <resultMap id="BaseResultMap" type="com.zhxjz.model.t_user.T_USER"> <id column="user_id" property="userId" jdbcType="CHAR" /> <result column="user_name" property="userName" jdbcType="VARCHAR" /> <result column="user_birthday" property="userBirthday" jdbcType="DATE" /> <result column="user_salary" property="userSalary" jdbcType="DOUBLE" /> </resultMap> <sql id="Base_Column_List"> user_id, user_name, user_birthday, user_salary </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String"> select <include refid="Base_Column_List" /> from t_user where user_id = #{userId,jdbcType=CHAR} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> delete from t_user where user_id = #{userId,jdbcType=CHAR} </delete> <insert id="insert" parameterType="com.zhxjz.model.t_user.T_USER"> insert into t_user (user_id, user_name, user_birthday, user_salary) values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE}, #{userSalary,jdbcType=DOUBLE}) </insert> <insert id="insertSelective" parameterType="com.zhxjz.model.t_user.T_USER"> insert into t_user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userId != null"> user_id, </if> <if test="userName != null"> user_name, </if> <if test="userBirthday != null"> user_birthday, </if> <if test="userSalary != null"> user_salary, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="userId != null"> #{userId,jdbcType=CHAR}, </if> <if test="userName != null"> #{userName,jdbcType=VARCHAR}, </if> <if test="userBirthday != null"> #{userBirthday,jdbcType=DATE}, </if> <if test="userSalary != null"> #{userSalary,jdbcType=DOUBLE}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.zhxjz.model.t_user.T_USER"> update t_user <set> <if test="userName != null"> user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="userBirthday != null"> user_birthday = #{userBirthday,jdbcType=DATE}, </if> <if test="userSalary != null"> user_salary = #{userSalary,jdbcType=DOUBLE}, </if> </set> where user_id = #{userId,jdbcType=CHAR} </update> <update id="updateByPrimaryKey" parameterType="com.zhxjz.model.t_user.T_USER"> update t_user set user_name = #{userName,jdbcType=VARCHAR}, user_birthday = #{userBirthday,jdbcType=DATE}, user_salary = #{userSalary,jdbcType=DOUBLE} where user_id = #{userId,jdbcType=CHAR} </update> </mapper>
8.*Controller.java测试
package com.zhxjz.controller.t_user; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.zhxjz.framework.model.json.JsonResult; import com.zhxjz.model.t_user.T_USER; import com.zhxjz.service.t_user.T_USERService; @Controller @RequestMapping("/t_user") public class T_USERController { @Autowired T_USERService t_userService; @RequestMapping("/getBean.do") public String getBean(String id, ModelMap model) { T_USER user = t_userService.getUserById(id); String message = new JsonResult(user).toJson(); model.put("message", message); return "message"; } @RequestMapping("/add.do") public String getBean(T_USER user, ModelMap model) { user.setUserBirthday(new Date()); t_userService.addUser(user); String message = new JsonResult().toJson(); model.put("message", message); return "message"; } }