九、Spring集成Mybatis

9.1添加jar包

9.2新建数据表
CREATE TABLE s_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
user_birthday DATE,
user_salary DOUBLE
)
9.3新建实体类

User.java

package com.domain;

import java.util.Date;

public class User {
    private int id;
    private String name;
    private Date birthday;
    private double salary;
    public User() {
        super();
    }
    public User(int id, String name, Date birthday, double salary) {
        super();
        this.id = id;
        this.name = name;
        this.birthday = birthday;
        this.salary = salary;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", birthday=" + birthday
                + ", salary=" + salary + "]";
    }   
}
9.4 DAO 接口: UserMapper

UserMapper.java

package com.mapper;

import java.util.List;

import com.domain.User;

public interface UserMapper {
    void save(User user);

    void update(User user);

    void delete(int id);

    User findById(int id);

    ListUser findAll();
}
9.5 SQL 映射文件: userMapper.xml

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"

!-- 
  namespace:必须与接口的全类名一致
  id:必须与对应接口的对应方法名一致
 --
mapper namespace="com.mapper.UserMapper"
   insert id="save" parameterType="User" 
      insert into s_user(user_name,user_birthday,user_salary) values(#{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_id id,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_id id,user_name name,user_birthday birthday,user_salary salary from s_user
   /select
/mapper
9.6 spring 的配置文件: beans.xml

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"
    !-- 1. 数据源 : DriverManagerDataSource --
    bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
       property name="url" value="jdbc:mysql://localhost:3306/mybatis" /
       property name="username" value="root" /
       property name="password" value="root" /
       property name="driverClassName" value="com.mysql.jdbc.Driver" /
    /bean

    !-- 2. mybatis 的 SqlSession 的工厂: SqlSessionFactoryBean
       dataSource:引用数据源
       typeAliasesPackage:指定实体类的包名,自动将实体类的简单类名映射成别名
     --
    bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
       property name="dataSource" ref="ds"/
       property name="typeAliasesPackage" value="com.domain"/
    /bean

    !-- 3. mybatis 自动扫描加载 Sql 映射文件 /接口: MapperScannerConfigurer
        basePackage:指定sql映射文件/接口所在的包(自动扫描)
        sqlSessionFactory:引用上面定义的sqlSessionFactory
     --
    bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
       property name="basePackage" value="com.mapper"/
       property name="sqlSessionFactory" ref="sqlSessionFactory"/
    /bean

    !-- 4. 事务管理 : DataSourceTransactionManager
      dataSource:引用上面定义的数据源
     --
    bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
       property name="dataSource" ref="ds"/
    /bean

    !-- 5. 使用声明式事务
       transaction-manager:引用上面定义的事务管理器
     --
    tx:annotation-driven transaction-manager="txManager"/
/beans
9.7 mybatis 的配置文件: mybatis-config.xml

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
9.8 测试类

SMTest.java

package com.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.domain.User;
import com.mapper.UserMapper;


@RunWith(SpringJUnit4ClassRunner.class)//使用spring的测试框架
@ContextConfiguration("/beans.xml") //加载spring的beans.xml配置文件 
public class SMTest {

    @Autowired
    private UserMapper userMapper;

    //测试添加
    @Test
    public void testAdd()
    {
        userMapper.save(new User(-1,"Tom",new Date(),3000));
    }

    //测试update
    @Test
    public void testUpdate()
    {
        userMapper.update(new User(1,"Tom2",new Date(),3000));
    }
}
最新回复(0)
/jishuyd4oQ3xE4QPYUJWj4ve0V0AXEDxD4aUaTKI9nQ_3D_3D4795110
8 简首页