以下分步骤来详细描述集成过程,伴随一个案例的编写过程:
1:添加 Jar 包
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
数据库驱动jar;
2:创建表:
CREATE TABLE s_user(
user_id number(10),
user_name VARCHAR(30),
user_birthday DATE,
user_salary number(10)
)
3:编写实体类: User.java,并产生set get方法;
private int id;
private String name;
private Date birthday;
private int salary;
4:编写DAO接口:UserMapper
public interface UserMapper
{
<span style="white-space:pre"> </span>void save(User user);
<span style="white-space:pre"> </span>void update(User user);
<span style="white-space:pre"> </span>void delete(int id);
<span style="white-space:pre"> </span>User findById(int id);
<span style="white-space:pre"> </span>List<User> findAll();
}
5:编写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">
<mapper namespace="com.cgc.spring_mybatis.mapper.UserMapper">
<insert id="save" parameterType="User">
insert into s_user(user_id,user_name,user_birthday,user_salary) values(#{id},#{name},#{birthday},#{salary})
</insert>
<update id="update" parameterType="User">
update s_user set user_name=#{name},user_birthday=#{birthday},user_salary=#{salary} where user_id=#{id}
</update>
<delete id="delete" parameterType="int">
delete from s_user where user_id=#{id}
</delete>
<select id="findById" parameterType="int" resultType="User">
select user_name name,user_birthday birthday ,user_salary salary from s_user where user_id=#{id}
</select>
<select id="findAll" parameterType="int" resultType="User">
select user_name name,user_birthday birthday ,user_salary salary from s_user
</select>
</mapper>
6:编写spring 的配置文件: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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1: 配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="scott"></property>
<property name="password" value="tiger"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:oracl"></property>
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
</bean>
<!-- 2:配置mybatis的sessionFactory,指定属性值
dataSource:指定数据源
typeAliasesPackage:指定实体类的包名,自动将实体类的简单类名映射成别名
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.cgc.spring_mybatis.domain"></property>
</bean>
<!-- 3:配置MapperScannerConfigurer,
属性:basePackage,配置Sql映射文件,接口所在的包
sqlSessionFactory:引用上边定义的sqlSessionFactory
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cgc.spring_mybatis.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 4:配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5:使用声明式事物 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
7:编写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>
8:编写调测类:
/*
* 文件名:Test.java
* 版权:Copyright by www.huawei.com
* 描述:
* 修改人:Cuigaochong
* 修改时间:2015-11-8
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.cgc.spring_mybatis.test;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cgc.spring_mybatis.domain.User;
import com.cgc.spring_mybatis.mapper.UserMapper;
/**
* <一句话功能简述>
* <功能详细描述>
*
* @author 姓名 工号
* @version [版本号, 2015-11-8]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test00
{
@Autowired
private UserMapper userMapper;
@Test
public void testAdd()
{
User user = new User(1, "Cui", new Date(), 100);
userMapper.save(user);
}
}