- 一:添加jar包
mybatis:
mybatis-3.2.0.jar
mybatis-spring-1.1.1.jar // 整合spring的关键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
//spring 以来的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
二:创建实体类及数据库对应的表
- public class User {
- private int id;
- private String name;
- private Date birthday;
- private double salary;
- //set,get方法
- }
- CREATE TABLE user(
- user_id INT AUTO_INCREMENT PRIMARY KEY,
- user_name VARCHAR(30),
- user_birthday DATE,
- user_salary DOUBLE
- )
三:mapper接口及对应的映射文件
UserMapper接口实现基本的crud功能
- import com.mybatis.beans.User;
- public interface UserMapper {
- void save(User user);
- void update(User user);
- void delete(int id);
- User findById(int id);
- List<User> findAll();
- }
- <pre name="code" class="html"><?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">
- <!-- namespace必须与接口的全类名一致-->
- <mapper namespace="com.mybatis.mapper.UserMapper">
- <!-- id必须和mapper接口对应的功能名称一致 ,此处只写save功能 -->
- <insert id="save" parameterType="User">
- insert into user(name, birth, salary) values(#{name},#{birth},#{salary})
- </insert>
- </mapper>
四:Spring的配置文件:beans.xml
- <pre name="code" class="html"><pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- mysql连接的基本配置,使用的是spring提供的 DriverManagerDataSource-->
- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="username" value="root"></property>
- <property name="password" value="13177180723"></property>
- <property name="url" value="jdbc:mysql:///mybatis"></property>
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <!-- 别名包,等同于mybatis配置文件conf.xml中的typeAliases,简化映射文件.xml中类的引用 -->
- <property name="typeAliasesPackage" value="com.mybatis.beans"></property>
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <!-- mapper接口扫描的包,即将此包下所有符合的mapper接口加载到SpringIOC容器管理 -->
- <property name="basePackage" value="com.mybatis.mapper"></property>
- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
- </bean>
- <!-- 添加事务 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 启动事务 -->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- </beans>
五: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>
六:测试
- <pre name="code" class="java">package com.mybatis.test;
- import static org.junit.Assert.*;
- 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.mybatis.beans.User;
- import com.mybatis.mapper.UserMapper;
- @RunWith(SpringJUnit4ClassRunner.class) //启用spring的测试框架
- @ContextConfiguration("/beans.xml") //加载spring配置文件
- public class MbtSpTest {
- @Autowired //自动注入mapper接口
- private UserMapper userMapper;
- @Test
- public void test() {
- User user = new User(-1, "Luxi", new Date(), 2000.00);
- userMapper.save(user);
- }
- }