mybatis基础之二
Posted 笨笨莱鸟学编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis基础之二相关的知识,希望对你有一定的参考价值。
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命名空间,就是对sql进行分类化管理 理解sql隔离 --> <mapper namespace="com.liu.mybatis.mapper.UserMapper"> <!-- 定义一个resultMap --> <resultMap type="user" id="userResultMap"> <id column="id_" property="id"/> <result column="address_" property="address"/> </resultMap> <!-- 定义sql片段 --> <sql id="user_query_where"> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=‘‘"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=‘‘"> and user.username like ‘%${userCustom.username}%‘ </if> </if> </sql> <!-- 在映射文件中配置sql语句 --> <!-- 通过ID查询sql记录 --> <!-- 通过select来执行查询 id: 标识映射文件中的sql 称为statement的id 将sql语句封装到mappedStatement中 parameterType:指定输入参数的类型 ,这里指定int型 #{}表示一个占位符 #{id} 表示接收输入的参数 id就是输入的参数名称 resultTypeL: 指定sql输出结果的javadui类型 --> <select id="findUserById" parameterType="int" resultType="com.liu.mybatis.po.User"> SELECT * FROM USER WHERE id = #{id} </select> <!-- 使用resultMap输出映射 --> <select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap"> SELECT id id_,address address_ FROM USER WHERE id = #{id} </select> <!-- ${}是一个拼接符号 --> <select id="findUserByName" parameterType="java.lang.String" resultType="user"> SELECT * FROM USER WHERE username like ‘%${value}%‘ </select> <!-- 用户信息综合查询 --> <select id="findUserList" parameterType="com.liu.mybatis.po.UserQueryVo" resultType="com.liu.mybatis.po.UserCustom"> select * from user <where> <if test="userCustom!=null"> <if test="userCustom.sex!=null and userCustom.sex!=‘‘"> and user.sex=#{userCustom.sex} </if> <if test="userCustom.username!=null and userCustom.username!=‘‘"> and user.username like ‘%${userCustom.username}%‘ </if> </if> <if test="ids!=null"> <!-- foreach实现sql的拼接 and id=1 or id=3 or id=5 --> <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"> id=#{user_id} </foreach> <!-- foreach实现sql的拼接 and id in (1,3,5) --> <!-- <foreach collection="ids" item="user_id" open="and id in (" close=")" separater=","> #{user_id} </foreach> --> </if> </where> </select> <!-- 用户信息总数查询 --> <select id="findUserCount" parameterType="com.liu.mybatis.po.UserQueryVo" resultType="int"> select * from user <where> <include refid="user_query_where"></include> </where> </select> <!-- selectkey获取自增主键 --> <insert id="insertUser" parameterType="com.liu.mybatis.po.User"> <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> SELECT LAST_INSERT_ID() </selectKey> insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address}) </insert> <delete id="deleteUser" parameterType="java.lang.Integer"> delete from user where id = #{id} </delete> <update id="updateUser" parameterType="com.liu.mybatis.po.User"> update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id} </update> </mapper>
UserMapper.java
package com.liu.mybatis.mapper; import java.util.List; import com.liu.mybatis.po.User; import com.liu.mybatis.po.UserCustom; import com.liu.mybatis.po.UserQueryVo; public interface UserMapper { public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception; public int findUserCount(UserQueryVo userQueryVo) throws Exception; public User findUserById(int id) throws Exception; public User findUserByIdResultMap(int id) throws Exception; public List<User> findUserByName(String username) throws Exception; public void insertUser(User user) throws Exception; public void deleteUser(int id) throws Exception; }
UserQueryVo.java
package com.liu.mybatis.po; import java.util.List; /* * 包装类型 */ public class UserQueryVo { private UserCustom userCustom; private List<Integer> ids; public UserCustom getUserCustom() { return userCustom; } public void setUserCustom(UserCustom userCustom) { this.userCustom = userCustom; } public List<Integer> getIds() { return ids; } public void setIds(List<Integer> ids) { this.ids = ids; } }
UserCustom.java
package com.liu.mybatis.po;
/*
* 用户的扩展类
*/
public class UserCustom extends User{
}
测试程序
package com.liu.mybatis.mapper; import static org.junit.Assert.*; 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.liu.mybatis.po.User; import com.liu.mybatis.po.UserCustom; import com.liu.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对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println(UserMapper.class); User user = userMapper.findUserById(28); System.out.println("20170902 =" + user); } @Test public void testFindUserByName() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findUserByName("晓春"); System.out.println("20170902 =" + list); } //用户综合信息查询 @Test public void testFindUserList() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); System.out.println("11111111111111111==================="); UserQueryVo userQueryVo =new UserQueryVo(); UserCustom userCustom = new UserCustom(); userCustom.setUsername("王晓春"); //userCustom.setSex("1"); List<Integer> ids= new ArrayList<Integer>(); ids.add(1); ids.add(28); ids.add(30); userQueryVo.setIds(ids); userQueryVo.setUserCustom(userCustom); List<UserCustom> list = userMapper.findUserList(userQueryVo); System.out.println("20170903 10:15 =" + list); } @Test public void testFindUserCount() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); UserQueryVo userQueryVo =new UserQueryVo(); UserCustom userCustom = new UserCustom(); userCustom.setUsername("王晓春"); userCustom.setSex("1"); userQueryVo.setUserCustom(userCustom); int count = userMapper.findUserCount(userQueryVo); System.out.println("20170902 23:15 =" + count); } @Test public void testFindUserByIdResultMap() throws Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); //创建UserMapper对象,mybatis自动生成mapper代理对象 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserByIdResultMap(28); System.out.println("20170902 ===" + user); } }
以上是关于mybatis基础之二的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取