程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> MyBatis使用總結+整合Spring,mybatisspring

MyBatis使用總結+整合Spring,mybatisspring

編輯:JAVA綜合教程

MyBatis使用總結+整合Spring,mybatisspring


  MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation 遷移到了google code,並且改名為MyBatis 。2013年11月遷移到Github。iBATIS一詞來源於“internet”和“abatis”的組合,是一個基於Java的持久層框架。

  MyBatis是支持普通SQL查詢,存儲過程和高級映射的優秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數的手工設置以及對結果集的檢索封裝。MyBatis可以使用簡單的XML或注解用於配置和原始映射,將接口和Java的POJO(Plain Old Java Objects,普通的Java對象)映射成數據庫中的記錄。

1 第一個MyBatis程序

  首先需要加入需要的jar包,構建Spring環境請參考:Spring學習之第一個hello world程序。這裡我們需要加入mybatis包和MySQL驅動包,使用IDEA環境來開發程序,最後工程加入的包如下圖所示:

  然後需要在test數據庫中新建測試表user,sql語句如下所示:

create table users (
    id int primary key auto_increment,
    name varchar(20),
    age int
);
insert into users (name, age) values('Tom', 12);
insert into users (name, age) values('Jack', 11);

1.1 定義表對應的實體類

public class User {

    private int id;
    private String name;
    private int age;

    public User() { }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

1.2 定義MyBatista的mybatisConfig.xml配置文件和user表的sql映射文件userMapper.xml

mybatisConfig.xml文件,該文件是在src目錄下新建的。

<?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>
<environments default="development">
    <!-- development:開發環境 work:工作模式 -->
    <environment id="development">
        <transactionManager type="JDBC" />
        <!-- 數據庫連接方式 -->
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://192.168.1.150/test" />
            <property name="username" value="root" />
            <property name="password" value="123456" />
        </dataSource>
    </environment>
</environments>

<!-- 注冊表映射文件 -->
<mappers>
    <mapper resource="com/mybatis/userMapper.xml"/>
</mappers>

</configuration>

userMapper.xml文件,該配置文件在com.mybatis包下,user表對應的實體類User也在com.mybatis包下。

<?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.mybatis.userMapper">
    <!-- 根據id查詢一個User對象 -->
    <select id="getUser" parameterType="int" resultType="com.mybatis.User">
        select * from users where id=#{id}
    </select>
    <select id="getUserAll" resultType="com.mybatis.User">
        select * from users
    </select>

    <!-- 插入一個User對象 -->
    <insert id="insertUser" parameterType="com.mybatis.User">
        insert into users (name, age) value(#{name}, #{age})
    </insert>

    <!-- 刪除一個User對象 -->
    <delete id="deleteUser" parameterType="int">
        delete from users where id=#{id}
    </delete>

    <!-- 更新一個User對象-->
    <update id="updateUser" parameterType="com.mybatis.User">
        update users set name=#{name}, age=#{age} where id=#{id}
    </update>
</mapper>

測試代碼如下:

public class mybaitstest {

    SqlSessionFactory sessionFactory = null;
    SqlSession sqlSession = null;

    {
        String resource = "mybatisConfig.xml";
        // 加載mybatis的配置文件(它也加載關聯的映射文件)
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 構建sqlSession的工廠
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        // 創建能執行映射文件中sql的sqlSession,默認是手動提交事務的,使用自動提交的話加上參數 true
        sqlSession = sessionFactory.openSession(true);
    }

    public void testSelectUser() {
        // 映射sql的標識字符串
        String statement = "com.mybatis.userMapper" + ".getUser";
        // 執行查詢返回一個唯一user對象的sql
        User user = sqlSession.selectOne(statement, 1);
        System.out.println(user);
    }

    public void testSelectAll() {
        List<User> users = sqlSession.selectList("com.mybatis.userMapper.getUserAll");
        System.out.println(users);
    }

    public void testInsertUser(User user) {
        int insert = sqlSession.insert("com.mybatis.userMapper.insertUser", user);
        // 如果不是自動提交的話,需要使用 sqlSession。commit()
        System.out.println(insert);
    }

    public void testDeleteUser(int id) {
        int delete = sqlSession.delete("com.mybatis.userMapper.deleteUser", id);
        System.out.println(delete);
    }

    public void testUpdateUser(User user) {
        int update = sqlSession.update("com.mybatis.userMapper.updateUser", user);
        System.out.println(update);
    }

    public static void main(String[] args) throws IOException {

        new mybaitstest().testSelectUser();
    }
}

最後輸出結果為:

 

2 基於注解的方式使用MyBatis

  基於注解的方式使用MyBatis,首先定義對應表的sql映射接口。

public interface IUserMapper {

    @Insert("insert into users (name, age) value(#{name}, #{age})")
    public int add(User user);

    @Delete("delete from users where id=#{id}")
    public int deleteById(int id);

    @Update("update users set name=#{name}, age=#{age} where id=#{id}")
    public int update(User user);

    @Select("select * from users where id=#{id}")
    public User getById(int id);

    @Select("select * from users")
    public List<User> getAll();
}

  然後在mybatisConfig.xml配置文件中注冊該接口:

<!-- 注冊表映射文件 -->
<mappers>
    <mapper class="com.mybatis.IUserMapper"/>
</mappers>

測試示例:

/**
 * 使用注解測試的方法
 */
public void test() {
    IUserMapper userMapper = sqlSession.getMapper(IUserMapper.class);

    User user = userMapper.getById(1);
    System.out.println(user);
}

 

3 如何簡化配置操作

  以上兩個程序示例都是直接在配置文件中寫連接數據庫的信息,其實還可以專門把數據庫連接信息寫到一個db.proteries文件中,然後由配置文件來讀取該db.properies文件信息。db.proteries文件內容如下:

user=root
password=123456
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://192.168.1.150/test

  然後在mybatisConfig.xml配置文件中將數據庫環境信息更改為如下所示:

<properties resource="db.properties"/>

<environments default="development">
    <!-- development:開發環境 work:工作模式 -->
    <environment id="development">
        <transactionManager type="JDBC" />
        <!-- 數據庫連接方式 -->
        <dataSource type="POOLED">
            <property name="driver" value="${driverClass}" />
            <property name="url" value="${jdbcUrl}" />
            <property name="username" value="${user}" />
            <property name="password" value="${password}" />
        </dataSource>
    </environment>
</environments>

  配置表對應的sql映射文件時,可以使用別名來簡化配置,在mybatisConfig.xml中添加如下配置,在userMapper中parameterType就可以配置為"_User"。

<typeAliases>
    <typeAlias type="com.mybatis.User" alias="_User"/>
</typeAliases>

 

4 字段名與實體類屬性名不匹配的沖突

  新建表和數據,在test數據庫中執行以下SQL語句:

create table orders (
    order_id int primary key auto_increment,
    order_no varchar(20),
    order_price float
);
insert into orders (order_no, order_price) values('aaa', 12);
insert into orders (order_no, order_price) values('bbb', 13);
insert into orders (order_no, order_price) values('ccc', 14);

  創建對應表的類:

public class Order { private int i; private String no; private float price; public Order() { } public Order(int i, String no, float price) { this.i = i; this.no = no; this.price = price; } public int getI() { return i; } public void setI(int i) { this.i = i; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } } View Code

  mybaitsConfig.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>

    <properties resource="db.properties"/>

    <typeAliases>
        <typeAlias type="com.mybatis.Order" alias="Order"/>
    </typeAliases>

    <environments default="development">
        <!-- development:開發環境 work:工作模式 -->
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 數據庫連接方式 -->
            <dataSource type="POOLED">
                <property name="driver" value="${driverClass}" />
                <property name="url" value="${jdbcUrl}" />
                <property name="username" value="${user}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>

    <!-- 注冊表映射文件 -->
    <mappers>
        <mapper resource="com/mybatis/orderMapper.xml"/>
    </mappers>

</configuration>

  接下來配置orderMapper.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.mybatis.orderMapper"> <!-- 根據id查詢一個Order對象 --> <select id="getOrder" parameterType="int" resultType="Order"> SELECT order_id id, order_no no, order_price price FROM orders WHERE order_id=#{id} </select> <!-- 這種解決字段與屬性沖突方式較常用 --> <select id="getOrder2" parameterType="int" resultType="Order" resultMap="getOrder2Map"> SELECT * FROM orders WHERE order_id=#{id} </select> <!-- resultMap 封裝映射關系 id 專門針對主鍵 result 針對一般字段 --> <resultMap id="getOrder2Map" type="Order"> <id property="id" column="order_id"/> <result property="no" column="order_price"/> <result property="price" column="order_price"/> </resultMap> </mapper> View Code

測試用例:

public class MyBaitsMain {
    SqlSessionFactory sessionFactory = null;
    SqlSession sqlSession = null;

    {
        String resource = "mybatisConfig.xml";
        // 加載mybatis的配置文件(它也加載關聯的映射文件)
        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 構建sqlSession的工廠
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        // 創建能執行映射文件中sql的sqlSession,默認是手動提交事務的,使用自動提交的話加上參數 true
        sqlSession = sessionFactory.openSession(true);
    }

    public static void main(String[] args) {
        String statement = "com.mybatis.orderMapper.getOrder";
        String statement2 = "com.mybatis.orderMapper.getOrder2";

        Order order = new MyBaitsMain().sqlSession.selectOne(statement, 2);
        System.out.println(order);
        order = new MyBaitsMain().sqlSession.selectOne(statement2, 2);
        System.out.println(order);
    }
}

輸出結果為:

 

5 實現關聯表查詢

5.1 一對一關聯

  這裡實現班級id查詢班級信息,班級信息中包括老師信息。首先創建表結構:

CREATE TABLE teacher( t_id INT PRIMARY KEY AUTO_INCREMENT, t_name VARCHAR(20) ); CREATE TABLE class( c_id INT PRIMARY KEY AUTO_INCREMENT, c_name VARCHAR(20), teacher_id INT ); ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id); INSERT INTO teacher(t_name) VALUES('LS1'); INSERT INTO teacher(t_name) VALUES('LS2'); INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1); INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2); View Code

  定義表對應的實體類:

public class Teacher { private int id; private String name; public Teacher() { } public Teacher(int id, String name) { this.id = id; this.name = name; } 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; } @Override public String toString() { return "Teacher{" + "id=" + id + ", name='" + name + '\'' + '}'; } } Teacher類 public class Classes { private int id; private String name; private Teacher teacher; public Classes() { } public Classes(int id, String name, Teacher teacher) { this.id = id; this.name = name; this.teacher = teacher; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Classes{" + "id=" + id + ", name='" + name + '\'' + ", teacher=" + teacher + '}'; } } Classes類

  定義sql映射文件,需要在mybatisConfig.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.mybatis.classesMapper"> <!-- 根據班級id查詢班級信息(包括老師信息) --> <select id="getClasses" parameterType="int" resultMap="ClassesMap"> SELECT * FROM class c, teacher t WHERE c.teacher_id = t.t_id and c.c_id = #{id} </select> <!-- 聯表查詢 --> <resultMap id="ClassesMap" type="com.mybatis.Classes"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" javaType="com.mybatis.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> </resultMap> <!-- 嵌套查詢 --> <select id="getClasses2" parameterType="int" resultMap="ClassesMap2"> SELECT * FROM class WHERE c_id=#{id} </select> <select id="getTeacher" parameterType="int" resultType="com.mybatis.Teacher"> SELECT t_id id, t_name FROM teacher WHERE t_id=#{id} </select> <resultMap id="ClassesMap2" type="com.mybatis.Classes"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" select="getTeacher"> </association> </resultMap> </mapper> classesMapper.xml文件

測試類:

public class MyBaitsMain { SqlSessionFactory sessionFactory = null; SqlSession sqlSession = null; { String resource = "mybatisConfig.xml"; // 加載mybatis的配置文件(它也加載關聯的映射文件) Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } // 構建sqlSession的工廠 sessionFactory = new SqlSessionFactoryBuilder().build(reader); // 創建能執行映射文件中sql的sqlSession,默認是手動提交事務的,使用自動提交的話加上參數 true sqlSession = sessionFactory.openSession(true); } public static void main(String[] args) { String statement = "com.mybatis.classesMapper.getClasses"; String statement2 = "com.mybatis.classesMapper.getClasses2"; Classes classes = new MyBaitsMain().sqlSession.selectOne(statement, 1); System.out.println(classes); classes = new MyBaitsMain().sqlSession.selectOne(statement2, 1); System.out.println(classes); } } MyBatisMain測試類

輸出結果:

5.2 一對多關聯

  這裡實現班級id查詢班級信息,班級信息中包括老師信息和學生信息。首先創建表結構:

CREATE TABLE student(
    s_id INT PRIMARY KEY AUTO_INCREMENT, 
    s_name VARCHAR(20), 
    class_id INT
);
INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_C', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_E', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);

  定義表對應的實體類:

public class Student { private int id; private String name; public Student(int id, String name) { this.id = id; this.name = name; } public Student() { } 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; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } } Student類

  定義sql映射文件,需要在mybatisConfig.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.mybatis.classesMapper2"> <select id="getClasses" parameterType="int" resultMap="ClassesMap"> SELECT * FROM class c, student s, teacher t WHERE c.c_id=s.class_id AND c.c_id=#{id} </select> <resultMap id="ClassesMap" type="com.mybatis.Classes"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" javaType="com.mybatis.Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <!-- collection: 做一對多關聯查詢的 ofType: 指定集合中元素對象的類型 --> <collection property="students" ofType="com.mybatis.Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap> <!-- 第二種方式 --> <select id="getClasses2" resultMap="ClassesMap2"> SELECT * FROM class WHERE c_id=#{id} </select> <select id="getTeacher" resultType="com.mybatis.Teacher"> SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} </select> <select id="getStudent" resultType="com.mybatis.Student"> SELECT s_id id, s_name name FROM student WHERE class_id=#{id} </select> <resultMap id="ClassesMap2" type="com.mybatis.Classes"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" select="getTeacher"> </association> <collection property="students" column="c_id" select="getStudent"> </collection> </resultMap> </mapper> classesMapper2.xml文件

  測試類:

public class MyBaitsMain { SqlSessionFactory sessionFactory = null; SqlSession sqlSession = null; { String resource = "mybatisConfig.xml"; // 加載mybatis的配置文件(它也加載關聯的映射文件) Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { e.printStackTrace(); } // 構建sqlSession的工廠 sessionFactory = new SqlSessionFactoryBuilder().build(reader); // 創建能執行映射文件中sql的sqlSession,默認是手動提交事務的,使用自動提交的話加上參數 true sqlSession = sessionFactory.openSession(true); } public static void main(String[] args) { String statement = "com.mybatis.classesMapper2.getClasses"; String statement2 = "com.mybatis.classesMapper2.getClasses2"; Classes classes = new MyBaitsMain().sqlSession.selectOne(statement, 1); System.out.println(classes); classes = new MyBaitsMain().sqlSession.selectOne(statement2, 1); System.out.println(classes); } } MyBaitsMain測試類

輸出結果:

 

6 MyBatis的緩存

正如大多數持久層框架一樣,MyBatis 同樣提供了一級緩存和二級緩存的支持

  • 一級緩存: 基於PerpetualCache 的 HashMap本地緩存,其存儲作用域為 Session,當 Session flush 或 close 之後,該Session中的所有 Cache 就將清空。
  • 二級緩存與一級緩存其機制相同,默認也是采用 PerpetualCache,HashMap存儲,不同在於其存儲作用域為 Mapper(Namespace),並且可自定義存儲源,如 Ehcache。
  • 對於緩存數據更新機制,當某一個作用域(一級緩存Session/二級緩存Namespaces)的進行了 C/U/D 操作後,默認該作用域下所有 select 中的緩存將被clear。

 

7 Spring集成MyBatis

  Spring集成MyBatis,開發環境為IDEA,打開IDEA,新建工程,工程名為spring-mybatis。注意,這裡我選擇的是帶有Web功能的工程,其實在Spring集成MyBatis示例中並沒有用到Web功能,這個可選可不選。

1、添加工程所需的jar包,比如MySQL驅動包、Spring包、commons-logging包等,最後添加的包結構圖如下:

  最後整個工程文件如下所示:

2、然後需要在test數據庫中新建測試表user,sql語句如下所示:

create table users (
    id int primary key auto_increment,
    name varchar(20),
    age int
);
insert into users (name, age) values('Tom', 12);
insert into users (name, age) values('Jack', 11);

3、定義表對應的實體類和表操作接口。

package com.luoxn28.test;

public class User {

    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package com.luoxn28.test;

import java.util.List;

public interface UserDao {

    public int insert(User user);

    public int update(User user);

    public int delete(int id);

    public User getById(int id);

    public List<User> getAll();
}

4、定義表映射配置文件userDao.xml和MyBatis配置文件mybatisConfig.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.luoxn28.test.UserDao">

    <insert id="insert" parameterType="com.luoxn28.test.User">
        INSERT users (name, age) VALUES (#{name}, #{age})
    </insert>

    <update id="update" parameterType="com.luoxn28.test.User">
        UPDATE users set name=#{name}, age=#{age} where id=#{id}
    </update>

    <delete id="delete" parameterType="int">
        DELETE FROM users where id=#{id}
    </delete>

    <select id="getById" parameterType="int" resultType="com.luoxn28.test.User">
        SELECT * FROM users WHERE id=#{id}
    </select>

    <select id="getAll" resultType="com.luoxn28.test.User">
        SELECT * FROM users
    </select>

</mapper>
<?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>
    <mappers>
        <mapper resource="com/luoxn28/test/userMapper.xml"/>
    </mappers>
</configuration>

5、配置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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 0.導入外部配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 1.配置數據源 DriverManagerDataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
        <property name="driverClassName" value="${driverClass}"/>
        <property name="url" value="${jdbcUrl}"/>
    </bean>

    <!-- 2.mybatis的SqlSession工廠  SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.luoxn28.test"/> <!-- 實體類包名,自動將實體類的簡單類名映射成為別名 -->
        <property name="configLocation" value="classpath:mybatisConfig.xml"/>
    </bean>

    <!-- 3.mybatis自動掃描加載sql映射文件 MapperScannerConfigurer
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.luoxn28.test"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean> -->

    <!-- 4.事務管理
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean> -->

    <!-- 5.聲明式事務 transaction-manager引用定義的事務管理器
    <tx:annotation-driven transaction-manager="transactionManager"/> -->

    <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.luoxn28.test.UserDao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>

</beans>

6、編寫測試類SMTest

package com.luoxn28.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SMTest {

    private ApplicationContext context = null;
    private UserDao userDao = null;

    @BeforeTest
    public void init() {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        userDao = (UserDao) context.getBean("userDao");
    }

    @Test
    public void testInsert() {
        System.out.println(userDao.insert(new User("luoxn28", 23)));
    }

    @Test
    public void testUpdate() {
        System.out.println(userDao.update(new User(10, "luoxn28", 22)));
    }

    @Test
    public void testDelete() {
        System.out.println(userDao.delete(10));
    }

    @Test
    public void testGetById() {
        System.out.println(userDao.getById(10));
    }

    @Test
    public void getGetAll() {
        System.out.println(userDao.getAll());
    }
}

輸出結果(這是測試的是getGetAll方法):

 

參考資料:

  1、尚硅谷-MyBatis學習視頻

  2、Spring學習之AOP總結帖

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved