MyBatisJava SSM框架之MyBatis3(二)——Mapper代理的开发格局

Mapper代理的付出规范

1、 mapper接口的全限定名要和mapper映射文件的namespace值一致。

2、 mapper接口的办法名称要和mapper映射文件的statement的id一致。

3、 mapper接口的章程参数类型要和mapper映射文件的statement的parameterType的值一致,而且它的参数是一个。

4、 mapper接口的情势重回值类型要和mapper映射文件的statement的resultType的值一致。

代码实战

1.搭建如下工程

MyBatis 1

 2.mapper接口——interface
UserMapper

package com.mf.mybatis.mapper;

import java.util.List;

import com.mf.mybatis.po.User;
import com.mf.mybatis.po.UserQueryVO;

public interface UserMapper {

    public User findUserById(int id) throws Exception;


    public void insertUser(User user) throws Exception;


    public List<User> findUserList(UserQueryVO vo);


    public int findUserCount(UserQueryVO vo);


    public User findUserRstMap(int id);
}

3.mapper映射文件——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>

    <properties resource="db.properties">
        <property name="db.username" value="123" />
    </properties>

    <typeAliases>
    <typeAlias type="com.mf.mybatis.po.User" alias="user"/>
        <package name="com.mf.mybatis.po" />
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}" />
                <property name="url" value="${db.url}" />
                <property name="username" value="${db.username}" />
                <property name="password" value="${db.password}" />
            </dataSource>
        </environment>
    </environments>


    <mappers>
       <!--<mapper resource="mapper/UserMapper.xml" /> -->
       <package name="com.mf.mybatis.mapper" />
    </mappers>

</configuration>

4.Db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=root

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

6.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.mf.mybatis.mapper.UserMapper">

    <select id="findUserById" parameterType="int" resultType="User">
        SELECT * FROM USER WHERE id =#{id}
    </select>

    <insert id="insertUser" parameterType="com.mf.mybatis.po.User">
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO USER(username,birthday,sex,address) VALUES(#{username},#{birthday},#{sex},#{address})
    </insert>

    <sql id="whereClause">
        <if test="user != null">
            <if test="user.username != null and user.username != ''">
                AND username LIKE '%${user.username}%'
            </if>
            <if test="user.sex != null and user.sex != ''">
                AND sex = #{user.sex}
            </if>
        </if>

        <if test="idList != null">
            AND id IN
            <foreach collection="idList" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </sql>


    <select id="findUserList" parameterType="com.mf.mybatis.po.UserQueryVO"    resultType="user">
        SELECT * FROM user
        <where>
            <include refid="whereClause" />
        </where>
    </select>

    <select id="findUserCount" parameterType="com.mf.mybatis.po.UserQueryVO" resultType="int">
        SELECT count(*) FROM user
        <where>
            <include refid="whereClause" />
        </where>
    </select>

    <resultMap type="user" id="UserRstMap">
        <id column="id_" property="id" />
        <result column="username_" property="username" />
        <result column="sex_" property="sex" />
    </resultMap>

    <select id="findUserRstMap" parameterType="int" resultMap="UserRstMap">
        Select id id_,username username_,sex sex_ from user where id = #{id}
    </select>
</mapper>

7.UserQueryVO.java

package com.mf.mybatis.po;

import java.util.List;


public class UserQueryVO {


    private User user;


    private List<Integer> idList;


    public List<Integer> getIdList() {
        return idList;
    }

    public void setIdList(List<Integer> idList) {
        this.idList = idList;
    }

    public User getUser() {
        return user;
    }

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

8.UserMapperTest.java

package com.mf.mybatis.mapper;

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

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 org.junit.Before;
import org.junit.Test;

import com.mf.mybatis.po.User;
import com.mf.mybatis.po.UserQueryVO;

public class UserMapperTest {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void setUp() throws Exception {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testFindUserById() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.findUserById(1);
        System.out.println(user);
        sqlSession.close();

    }

    @Test
    public void testInsertUser() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = new User();
        user.setUsername("sgz");
        user.setAddress("北京");
        mapper.insertUser(user);
        System.out.println(user.getId());
        sqlSession.commit();
        sqlSession.close();

    }

    @Test
    public void testFindUserList() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserQueryVO vo = new UserQueryVO();
        // User user= new User();
        // user.setUsername("沐风");
        // user.setSex("1");
        // vo.setUser(user);
        List<Integer> idList = new ArrayList<>();
        idList.add(1);
        idList.add(2);
        idList.add(10);
        vo.setIdList(idList);

        List<User> list = mapper.findUserList(vo);
        int count = mapper.findUserCount(vo);

        System.out.println(list);
        System.out.println(count);
        sqlSession.close();
    }

    @Test
    public void testFindUserRstMap() throws Exception {

        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.findUserRstMap(1);
        System.out.println(user);
        sqlSession.close();
    }

}

 

 

 

网站地图xml地图