Mybatis学习笔记二

本篇内容,紧接上一篇内容 Mybatis学习笔记一

输入映射和输出映射

传递简单类型和pojo类型上篇已介绍过,下面介绍一下包装类型。

传递pojo包装对象

开发中通过可以使用pojo传递查询条件。查询条件可能是综合的查询条件,不仅包括用户查询条件还包括其它的查询条件(比如查询用户信息的时候,将用户购买商品信息也作为查询条件),这时可以使用包装对象传递输入参数。包装对象即Pojo类中的一个属性是另外一个pojo。

演示:根据用户名模糊查询用户信息,查询条件放到QueryVo的user属性中。

1、编写QueryVo

package com.yyb.pojo;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/8/16.
 */
public class QueryVo implements Serializable{
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    private User user;

}

2、userMapper配置

    <select id="findByQueryVo"  parameterType="QueryVo" resultType="User">
        SELECT * FROM user WHERE username  like "%"#{user.username}"%"
    </select>

3.、userMapper接口中添加

 List<User> findByQueryVo(QueryVo vo);

4、测试类

    @org.junit.Test
    public void func2() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        QueryVo vo=new QueryVo();
        User u=new User();
        u.setUsername("五");
        vo.setUser(u);
        List<User> us = userMapper.findByQueryVo(vo);
        for (User use:us) {
            System.out.println(use);
        }
    }

resultType(输出类型)

1、输出简单类型。示例:查询用户表数据条数

在userMapper.xml中添加如下代码:

   <select id="findUserCount"  resultType="Integer">
        SELECT COUNT(*) FROM user
    </select>

在userMapper接口中添加以下代码:

 int findUserCount();

测试方法

    @org.junit.Test
    public void func3() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int count = userMapper.findUserCount();
        System.out.println(count);
    }

注意:输出简单类型必须查询出来的结果集有一条记录,最终将第一个字段的值转换为输出类型。

2、输出pojo对象,参考上篇

3、输出pojo列表,参考上篇

4、resultMap

resultType可以指定将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。

resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

示例:查询订单表order的所有数据

添加Order实体

package com.yyb.pojo;

import java.io.Serializable;
import java.util.Date;

public class Orders  implements Serializable{
    private static final long serialVersionUID = 1L;

    private Integer id;

    private Integer userId;

    private String number;

    private Date createtime;

    private String note;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number == null ? null : number.trim();
    }

    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note == null ? null : note.trim();
    }

    
    
}
Orders .java

添加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.yyb.mapper.OrderMapper" >
    <resultMap id="orders" type="Orders" >
        <!--<id column="id" property="id"></id>-->
      <result column="user_id" property="userId" ></result>
    </resultMap>
    <select id="queryOrderList" resultMap="orders">
       SELECT id,user_id,number,createtime, note FROM orders
    </select>
</mapper>

添加orderMapper接口,在其中添加以下代码:

package com.yyb.mapper;

import com.yyb.pojo.Orders;

import java.util.List;

public interface OrderMapper {
    List<Orders> queryOrderList();
}

测试类

package com.yyb.test;

import com.yyb.mapper.OrderMapper;
import com.yyb.pojo.Orders;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */
public class TestOrder {
    @org.junit.Test
    public void func1() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        OrderMapper userMapper = sqlSession.getMapper(OrderMapper.class);
        List<Orders> orders = userMapper.queryOrderList();
        for (Orders o:orders) {
            System.out.println(o);
        }
    }
}

由于sql查询列(user_id)和Order类属性(userId)不一致,所以查询结果不能映射到pojo中。需要定义resultMap,把orderResultMap将sql查询列(user_id)和Order类属性(userId)对应起来。

动态sql

通过mybatis提供的各种标签方法实现动态拼接sql。

示例:根据性别和名字查询用户

在userMapper接口中添加如下代码:

 List<User> findUserByWhere(User user);

在userMapper.xml中添加如下代码:

 <select id="findUserByWhere" parameterType="User" resultType="User">
        SELECT id, username, birthday, sex, address FROM user
        WHERE 1=1
        <if test="sex != null and sex != ''">
            AND sex = #{sex}
        </if>
        <if test="username != null and username != ''">
            AND username LIKE "%"#{username}"%"
        </if>
    </select>

测试方法如下:

@org.junit.Test
    public void func4() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User u=new User();
        u.setUsername("张");
        //u.setSex("1");
        List<User> us = userMapper.findUserByWhere(u);
        for (User use:us) {
            System.out.println(use);
        }
    }

If标签

注意字符串类型的数据需要要做不等于空字符串校验。

        WHERE 1=1
        <if test="sex != null and sex != ''">
            AND sex = #{sex}
        </if>
        <if test="username != null and username != ''">
            AND username LIKE "%"#{username}"%"
        </if>

Where标签

上面的sql还有where 1=1 这样的语句,很麻烦,可以使用where标签进行改造,where标签可以自动添加where,同时处理sql语句中第一个and关键字。

改造UserMapper.xml,如下:

 <select id="findUserByWhere" parameterType="User" resultType="User">
        SELECT id, username, birthday, sex, address FROM user
        <where>
            <if test="sex != null">
                AND sex = #{sex}
            </if>
            <if test="username != null and username != ''">
                AND username LIKE '%${username}%'
            </if>
        </where>
    </select>

Sql片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的。

把上面例子中的id, username, birthday, sex, address提取出来,作为sql片段,如下:

<!--sql片段-->
<sql id="selector">
    SELECT id, username, birthday, sex, address FROM user
</sql>
 <select id="findUserByWhere" parameterType="User" resultType="User">
        <include refid="selector"/>
        <where>
            <if test="sex != null">
                AND sex = #{sex}
            </if>
            <if test="username != null and username != ''">
                AND username LIKE '%${username}%'
            </if>
        </where>
    </select>

如果要使用别的Mapper.xml配置的sql片段,可以在refid前面加上对应的Mapper.xml的namespace,例如下图

foreach标签

向sql传递数组或List,mybatis使用foreach解析。

示例:根据多个id查询用户信息

添加接口 findUserByIds

改造QueryVo

package com.yyb.pojo;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */
public class QueryVo implements Serializable{
    private User user;

    List<Integer> idsList;

    Integer[] ids;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
    public List<Integer> getIdsList() {
        return idsList;
    }
    public void setIdsList(List<Integer> idsList) {
        this.idsList = idsList;
    }
    public Integer[] getIds() {
        return ids;
    }
    public void setIds(Integer[] ids) {
        this.ids = ids;
    }
}
View Code

UserMapper.xml文件

 <select id="findUserByIds" parameterType="QueryVo" resultType="User">
        <include refid="selector"/>
        <where>
            <!-- foreach标签,进行遍历 -->
            <!-- collection:遍历的集合,这里是QueryVo的ids属性,当不使用包装类,直接传递数组或者list时,collection="Array" 或者collection="list" -->
            <!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
            <!-- open:在前面添加的sql片段 -->
            <!-- close:在结尾处添加的sql片段 -->
            <!-- separator:指定遍历的元素之间使用的分隔符 -->
            <foreach collection="idsList" item="item" open="id IN (" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </select>

测试方法:

    @org.junit.Test
    public void func5() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
       QueryVo vo=new QueryVo();
       List<Integer>list=new ArrayList<>();
        list.add(16);
        list.add(24);
        list.add(30);
        vo.setIdsList(list);
        List<User> us = userMapper.findUserByIds(vo);
        for (User use:us) {
            System.out.println(use);
        }
    }

关联查询

商品订单数据模型

 

方式一:定义专门的pojo类作为输出类型,其中定义了sql查询结果集所有的字段(继承现有pojo)。此方法较为简单,企业中使用普遍。

下面介绍方式二,使用resultMap,定义专门的resultMap用于映射一对一查询结果。

一对一查询

示例:查询所有订单信息,关联查询下单用户信息。

注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。

在OrderMapper添加接口   List<Orders> queryOrderUser(); 

在Order类中添加如下代码:

  //在Order类中加入User属性,user属性中用于存储关联查询的用户信息,因为订单关联查询用户是一对一关系,所以这里使用单个User对象存储关联查询的用户信息。    
private
User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; }

在OrderMapper.xml中添加如下代码:

 <resultMap id="orderuser" type="Orders">
        <id column="id" property="id"></id>
        <result column="user_id" property="userId"></result>
        <result column="number" property="number"></result>
        <result column="createtime" property="createtime"></result>
        <result column="note" property="note"></result>

      <!-- association :配置一对一属性 -->

      <!-- property:order里面的User属性名 -->

      <!-- javaType:属性类型 -->

        <association property="user" javaType="User">
            <id column="user_id" property="id"></id>
            <result column="username" property="username"></result>
            <result column="sex" property="sex"></result>
            <result column="birthday" property="birthday"></result>
            <result column="address" property="address"></result>
        </association>
    </resultMap>
    <select id="queryOrderUser" resultMap="orderuser">
       SELECT
            o.id,o.user_id,o.number,o.createtime, o.note ,
            u.username,u.sex,u.birthday,u.address
        FROM orders  o
        LEFT JOIN USER u
        ON o.user_id=u.id
    </select>

测试代码:

 @org.junit.Test
    public void func2() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        OrderMapper userMapper = sqlSession.getMapper(OrderMapper.class);
        List<Orders> orders = userMapper.queryOrderUser();
        for (Orders o:orders) {//打断点调试查看user对象的值
            System.out.println(o);
        }
    }

 一对多查询

案例:查询所有用户信息及用户关联的订单信息。

用户信息和订单信息为一对多关系。

修改pojo类,在User类中加入List<Order>属性,如下:

    private List<Orders>lsOrders;

    public List<Orders> getLsOrders() {
        return lsOrders;
    }

    public void setLsOrders(List<Orders> lsOrders) {
        this.lsOrders = lsOrders;
    }

在userMapper接口中添加如下接口:  List<Orders> queryOrderUser(); 

在UserMapper.xml添加如下代码:

<resultMap id="userorder" type="User">
        <id column="user_id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="sex" property="sex"></result>
        <result column="birthday" property="birthday"></result>
        <result column="address" property="address"></result>

        <collection property="lsOrders" ofType="Orders">
            <id column="id" property="id"></id>
            <result column="user_id" property="userId"></result>
            <result column="number" property="number"></result>
            <result column="createtime" property="createtime"></result>
            <result column="note" property="note"></result>
        </collection>
    </resultMap>
    <select id="findUserOrders" resultMap="userorder">
        SELECT
            u.username,u.sex,u.birthday,u.address,
            o.id,o.user_id,o.number,o.createtime, o.note
        FROM USER u
        LEFT JOIN orders  o
        ON o.user_id=u.id
    </select>

测试代码如下:

@org.junit.Test
    public void func6() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //SqlSEssion帮我生成一个实现类  (给接口)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> us = userMapper.findUserOrders();
        for (User u:us) {
            System.out.println(u);
        }
    }

Mybatis整合spring

整合思路

  1. SqlSessionFactory对象应该放到spring容器中作为单例存在。
  2. 传统dao的开发方式中,应该从spring容器中获得sqlsession对象。
  3. Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
  4. 数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

整合需要的jar包

  1. spring的jar包
  2. Mybatis的jar包
  3. Spring+mybatis的整合包。
  4. Mysql的数据库驱动jar包。
  5. 数据库连接池的jar包。

jar包如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yyb</groupId>
    <artifactId>ssm_crm</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ssm_crm Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.1.3.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.7</version>
        </dependency>
        <!--mybatis-spring适配器 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--Spring框架核心库 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <!--spring-context,spring-core,spring-expression,spring-aop以及spring-beans-->
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--spring-orm,spring-jdbc和spring-tx-->
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <!--spring-web ,spring-webmvc-->
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.8</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>mybatis</finalName>
        <!--配置jdk版本为1.8-->
        <plugins>
            <!--解决自定义标签被覆盖-->
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <classesDirectory>target/classes/</classesDirectory>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <!-- 在打包成功后使用tomcat:run来运行服务 -->
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

加入配置文件

1、SqlMapConfig.xml,在resource目录下创建SqlMapConfig.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>
    <!-- 设置别名 -->
    <typeAliases>
        <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
        <package name="com.yyb.pojo" />
    </typeAliases>
</configuration>

2、applicationContext.xml,在resource目录下创建applicationContext.xml,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- 配置mybatis的工厂 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

3、db.properties,在resource文件加下创建db.properties文件,内容如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybitis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456

4、log4j.properties,在resource文件加下创建log4j.properties文件,内容如下:

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Dao的开发

两种dao的实现方式:

1、原始dao的开发方式

原始的DAO开发接口+实现类来完成。需要dao实现类需要继承SqlsessionDaoSupport类。创建UserDao接口文件和UserDaoImpl.java文件。实现类的代码如下:

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
}

在applicationContext中添加如下代码:

 <bean id="userDao" class="com.yyb.dao.UserDaoImpl" >
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
    </bean>

2、使用Mapper代理形式开发方式

a)直接配置Mapper代理

    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
        <property name="mapperInterface" value="com.yyb.mapper.UserMapper"></property>
    </bean>

测试代码:

  @org.junit.Test
    public void func1() throws Exception {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //UserMapper mapper= (UserMapper) ac.getBean("userMapper");
        UserMapper mapper=ac.getBean(UserMapper.class);
        User user = mapper.findUserById(10);
        System.out.println(user);
    }

b)使用扫描包配置Mapper代理

  <!--mapper动态代理扫描-->
   <bean id="userMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.yyb.mapper"></property>
   </bean>

Mybatis逆向工程

使用官方网站的Mapper自动生成工具mybatis-generator-core-1.3.2来生成pojo类和Mapper映射文件。

详情参考这里

注意:

1.逆向工程生成的代码只能做单表查询

2.不能在生成的代码上进行扩展,因为如果数据库变更,需要重新使用逆向工程生成代码,原来编写的代码就被覆盖了。

3.一张表会生成4个文件

 

mybatis中文文档地址

本文源码下载地址

SSM整合例子

posted @ 2017-08-15 18:46  ~冰  阅读(591)  评论(0编辑  收藏  举报