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基础学习(二)—开发Dao方式
一.原始Dao开发方式 UserDao.java public interface UserDao{ public User findUserByID(Serializable id); public ...
- JAVA之Mybatis基础入门二 -- 新增、更新、删除
上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 1.首先编写USER.XML(表的xml)使用insert元素,元素写在map ...
- Mybatis基础(二)
Mybatis连接池 Mybatis连接池提供了三种配置方式,配置的位置在SqlMapConfig.xml的dataSource标签中,其type属性就是配置连接池的种类.type的可取值 1.POO ...
- MyBatis基础入门《二十》动态SQL(foreach)
MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...
- MyBatis基础入门《十二》删除数据 - @Param参数
MyBatis基础入门<十二>删除数据 - @Param参数 描述: 删除数据,这里使用了@Param这个注解,其实在代码中,不使用这个注解也可以的.只是为了学习这个@Param注解,为此 ...
- MyBatis基础入门《二》Select查询
MyBatis基础入门<二>Select查询 使用MySQL数据库,创建表: SET NAMES utf8mb4; ; -- ---------------------------- -- ...
- mybatis基础系列(二)——基础语法、别名、输入映射、输出映射
增删改查 mapper根节点及其子节点 mybatis框架需要读取映射文件创建会话工厂,映射文件是以<mapper>作为根节点,在根节点中支持9个元素,分别为insert.update.d ...
- myBatis 基础测试 表关联关系配置 集合 测试
myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...
- mybatis入门系列二之输入与输出参数
mybatis入门系列二之详解输入与输出参数 基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...
随机推荐
- new Date('2014/04/30') 和 new Date('2014-04-30') 的区别
new Date('2014/04/30') Wed Apr 30 2014 00:00:00 GMT+0800 (中国标准时间) new Date('2014-04-30'); Wed Apr 30 ...
- asp.net实现access数据库分页
最近在编程人生上看到篇文章很有感触,觉得人生还是要多奋斗.今天给大家贡献点干货. <divclass="page"id="ctrlRecordPage"& ...
- 游戏的物理和数学:Unity中的弹道和移动目标提前量计算
下载地址:https://www.jianguoyun.com/p/DZPN6ocQ2siRBhihnx8 弹道计算是游戏里常见的问题,其中关于击中移动目标的自动计算提前量的话题,看似简单,其实还是挺 ...
- device tree --- #address-cells and #size-cells property
device tree source Example1 / { #address-cells = <0x1>; // 在 root node 下使用 1 個 u32 來代表 address ...
- 2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest
2017 ACM - ICPC Asia Ho Chi Minh City Regional Contest A - Arranging Wine 题目描述:有\(R\)个红箱和\(W\)个白箱,将这 ...
- 2017 NWERC
2017 NWERC Problem A. Ascending Photo 题目描述:给出一个序列,将其分成\(m\)份(不需要均等),使得将这\(m\)份重新排列后构成的是不下降序列,输出最小的\( ...
- c json实战引擎六 , 感觉还行
前言 看到六, 自然有 一二三四五 ... 为什么还要写呢. 可能是它还需要活着 : ) 挣扎升级中 . c json 上面代码也存在于下面项目中(维护的最及时) structc json 这次版本 ...
- 如何使用curl命令指定ip访问url
有时我们需要测试一个url,但域名并没解析,这时为了一个简单的测试而写host或去做域名解析,显然这并不高效,而有些域名甚至是正式的域名,因此我们可有使用curl命令进行测试 方法一 curl url ...
- C语言实现int转换string
#include <stdio.h> #include <stdlib.h> #include <string.h> int string2int(const ch ...
- SYN Flood攻击及防御方法 (转)
原文连接:http://blog.csdn.net/bill_lee_sh_cn/article/details/6065704 一.为什么Syn Flood会造成危害 这要从操作系统的TC ...