Spring整合Mybatis,springmybatis
jar檔案:http://download.csdn.net/detail/huhui_bj/8575533
一、準備jar包
【mybatis】
mybatis-3.2.0.jar
mybatis-spring-1.1.1.jar
log4j-1.2.17.jar
【spring】
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-test-3.2.4.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
aopalliance-1.0.jar
cglib-nodep-2.2.3.jar
commons-logging-1.1.1.jar
【MYSQL驅動包】
mysql-connector-java-5.0.4-bin.jar
二、建立資料表和實體類
CREATE TABLE s_user(user_id INT AUTO_INCREMENT PRIMARY KEY,user_name VARCHAR(30),user_birthday DATE,user_salary DOUBLE)
public class User {private int id;private String name;private Date birthday;private double salary; //set,get方法}
三、DAO介面
public interface UserMapper {void save(User user);void update(User user);void delete(int id);User findById(int id);List<User> findAll();}
四、SQL對應檔:userMapper.xml(與介面忽略大小寫同名)
<?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"><!-- UserMapper.java的檔案名稱必須和userMapper.xml相同namespace:必須與對應的介面全類名一致id:必須與對應介面的某個方法名一致 --><mapper namespace="com.atguigu.mybatis.test9.UserMapper"><resultMap type="User" id="userResult"><result column="user_id" property="id"/><result column="user_name" property="name"/><result column="user_birthday" property="birthday"/><result column="user_salary" property="salary"/></resultMap><!-- 取得插入資料後的id --><insert id="save" keyColumn="user_id" keyProperty="id" useGeneratedKeys="true">insert into s_user(user_name,user_birthday,user_salary)values(#{name},#{birthday},#{salary})</insert><update id="update">update s_userset user_name = #{name},user_birthday = #{birthday},user_salary = #{salary}where user_id = #{id}</update><delete id="delete">delete from s_userwhere user_id = #{id}</delete><select id="findById" resultMap="userResult">select *from s_userwhere user_id = #{id}</select><select id="findAll" resultMap="userResult">select * from s_user</select></mapper>
五、Spring設定檔beans.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" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- 1. 資料來源 : DriverManagerDataSource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatis"/><property name="username" value="root"/><property name="password" value="root"/></bean><!-- 2. mybatis的SqlSession的工廠: SqlSessionFactoryBean dataSource / typeAliasesPackage--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="typeAliasesPackage" value="com.atuigu.spring_mybatis2.domain"/></bean><!-- 3. mybatis自動掃描載入Sql對應檔 : MapperScannerConfigurer sqlSessionFactory / basePackage--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.atuigu.spring_mybatis2.mapper"/><property name="sqlSessionFactory" ref="sqlSessionFactory"/></bean> <!-- 4. 交易管理 : DataSourceTransactionManager --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean> <!-- 5. 使用聲明式事務 --><tx:annotation-driven transaction-manager="txManager" /></beans>
六、mybatis的設定檔: mybatis-config.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><!-- Spring整合myBatis後,這個設定檔基本可以不要了--><!-- 設定外部設定檔 --><!-- 設定類別名 --><!-- 設定資料庫連接環境 --><!-- 對應檔 --></configuration>
七、測試
@RunWith(SpringJUnit4ClassRunner.class) //使用Springtest測試架構@ContextConfiguration("/beans.xml") //載入配置public class SMTest {@Autowired //注入private UserMapper userMapper;@Testpublic void save() {User user = new User();user.setBirthday(new Date());user.setName("marry");user.setSalary(300);userMapper.save(user);System.out.println(user.getId());}@Testpublic void update() {User user = userMapper.findById(2);user.setSalary(2000);userMapper.update(user);}@Testpublic void delete() {userMapper.delete(3);}@Testpublic void findById() {User user = userMapper.findById(1);System.out.println(user);}@Testpublic void findAll() {List<User> users = userMapper.findAll();System.out.println(users);}}