Mybatis培训教程

10. spring集成mybatis

10.1. 添加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

 

10.2. 数据库表

CREATE TABLE s_user(

user_id INT AUTO_INCREMENT PRIMARY KEY,

user_name VARCHAR(30),

user_birthday DATE,

user_salary DOUBLE

)

 

10.3. 编码:

2). 实体类: User

public class User {

 

private int id;

private String name;

private Date birthday;

private double salary;

 

    //set,get方法

}

 

3). DAO接口: UserMapper (XXXMapper)

public interface UserMapper {

 

void save(User user);

void update(User user);

void delete(int id);

User findById(int id);

List<User> findAll();

}

 

4). 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.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_user

set user_name = #{name},

user_birthday = #{birthday},

user_salary = #{salary}

where user_id = #{id}

</update>

<delete id="delete">

delete from s_user

where user_id = #{id}

</delete>

 

<select id="findById" resultMap="userResult">

select *

from s_user

where user_id = #{id}

</select>

<select id="findAll" resultMap="userResult">

select *

from s_user

</select>

 

</mapper>

 

5). 数据库连接文件: db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/mybatis

jdbc.username=root

jdbc.password=root

 

6). 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/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

 

<!-- 关联properties文件  -->

<context:property-placeholder location="db.properties"  />

 

<!-- 数据源 -->

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource"

p:driverClassName="${jdbc.driverClassName}"

p:url="${jdbc.url}"

p:username="${jdbc.username}"

p:password="${jdbc.password}"/>

 

<!--

class: 指定用来创建sqlSession的工厂

dataSource-ref: 使用的数据源

typeAliasesPackage: 自动扫描的实体类包

 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"

p:dataSource-ref="dataSource"

p:typeAliasesPackage="org.monmday.springmybatis.domian"/>

 

<!--

class : 指定自动扫描xxxMapper.xml映射文件的类

basePackage: 自动扫描的配置包

-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"

p:basePackage="org.monmday.springmybatis.mappers"

p:sqlSessionFactoryBeanName="sqlSessionFactory"/>

 

 

<!-- 事务管理 -->

<bean id="txManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource"/>

<tx:annotation-driven transaction-manager="txManager" />

</beans>

 

7). 测试

@RunWith(SpringJUnit4ClassRunner.class) //使用Springtest框架

@ContextConfiguration("/beans.xml") //加载配置

public class SMTest {

 

@Autowired  //注入

private UserMapper userMapper;

 

@Test

public void save() {

User user = new User();

user.setBirthday(new Date());

user.setName("marry");

user.setSalary(300);

userMapper.save(user);

System.out.println(user.getId());

}

 

@Test

public void update() {

User user = userMapper.findById(2);

user.setSalary(2000);

userMapper.update(user);

}

 

@Test

public void delete() {

userMapper.delete(3);

}

 

@Test

public void findById() {

User user = userMapper.findById(1);

System.out.println(user);

}

 

@Test

public void findAll() {

List<User> users = userMapper.findAll();

System.out.println(users);

}

}

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源,欢迎大家关注尚硅谷公众号(atguigu)了解更多。