parameterType指定参数类型
基本类型参数(int、string.......)
pojo类型:user对象
map类型
包装类型
1、map类型的传递
需求:查询用户性别为男,姓张的用户
namespace="com.itcast.dao.UserMapper" >
select * from user where sex=#{sex} and username like "%"#{username}"%"
@Test
public void fun1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("sex", "男");
map.put("username", "张");
List<User> list = userMapper.findUserWithMap(map);
System.out.println(list);
}
2、包装类型的传递
需求:查询用户性别为男,姓张的用户
select * from user where sex=#{user.sex} and username like "%"#{user.username}"%"
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = userMapper.findUserWithQueryVo(queryVo);
System.out.println(list);
}
基本类型
pojo类型
返回集合类型
1、返回基本类型
需求:查询用户性别为男,姓张的用户总计数
select count(2) from user where sex=#{user.sex} and username like "%"#{user.username}"%"
@Test
public void fun3() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
int count = userMapper.findUserWithQueryVoCount(queryVo);
System.out.println(count);
}
2、resultType特性
特性:查询数据库列名必须和映射的javabean属性名称一一对应,且名称相同,否则不能映射成功
验证:
select id _id,username _username,birthday _birthday,sex _sex,address _address from user
@Test
public void fun4() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list= userMapper.findUserWithResultType();
System.out.println(list);
}
返回结果错误:[null, null, null, null, null, null, null, null, null]
解决映射不成功的方案:
* 定义别名
* resultMap
解决resultType 数据库列名与javabean属性名不相同不能映射的问题
需要先定义一个resultMap的映射关系
主键映射 :使用id
column:查询数据库列名
property:javabean属性名
type="user" id="userMap">
column="_id" property="id" />
column="_username" property="username" />
column="_birthday" property="birthday" />
column="_sex" property="sex" />
column="_address" property="address" />
select id _id,username _username,birthday _birthday,sex _sex,address _address from user
java代码和上面一样
1、多的一方 一对一查询
可以用resultType也可以用resultMap查询
① 用resultType来映射
需求:通过订单查询用户和订单所有数据,一个订单只对应一个用户,所以是一对一查询
新定义一个订单实体类,再用一个OrdersCustom类继承Orders类中所有属性,再添加User类中的所有属性,注意把User类中的主键名字改成uid,因为不能和Orders的主键id名字重复啊,查询后的结果就封装在OrdersCustom这个类的属性中
namespace="com.itcast.dao.OrdersMapper" >
select o.id,o.user_id userId,o.number,o.createtime createTime,o.note,
u.id uid,u.username,u.birthday,u.sex,u.address
from user u,orders o where u.id=o.user_id
@Test
public void fun1() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
List<OrdersCustom> list = ordersMapper.findOrdersWithUserByResultType();
System.out.println(list);
}
②用resultMap来映射
配置一对一关系映射:association
property:指定映射orders中那个属性
javaType:指定关系映射对象类型
namespace="com.itcast.dao.OrdersMapper" >
type="orders" id="ordersMap">
column="id" property="id" />
column="user_id" property="userId" />
column="number" property="number" />
column="createtime" property="createTime" />
column="note" property="note" />
property="user" javaType="user">
column="uid" property="id" />
column="username" property="username" />
column="birthday" property="birthday" />
column="sex" property="sex" />
column="address" property="address" />
SELECT orders.*,user.id uid,user.username,user.birthday,user.sex,user.address
FROM orders,user WHERE orders.user_id = user.id
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
OrdersMapper ordersMapper = sqlSession.getMapper(OrdersMapper.class);
List<Orders> list = ordersMapper.findOrdersWithUserByResultMap();
System.out.println(list);
}
2、一的一方 一对多查询
一个用户对应了多个订单,如果通过用户查询用户数据和对应的订单数据,那就是一对多查询
一对多查询只能用resultMap来映射
namespace="com.itcast.dao.UserMapper" >
type="user" id="userMap">
column="uid" property="id" />
column="username" property="username"/>
column="birthday" property="birthday"/>
column="sex" property="sex"/>
column="address" property="address"/>
property="oList" ofType="orders" >
column="id" property="id"/>
column="user_id" property="userId"/>
column="number" property="number"/>
column="createtime" property="createTime"/>
column="note" property="note"/>
select user.id uid,user.username,user.birthday,user.sex,user.address,
orders.id,orders.user_id userId,orders.number,orders.createtime createTime,orders.note
from user,orders where orders.user_id=user.id
@Test
public void fun3() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> list = userMapper.findUserWithOrdersByMap();
System.out.println(list);
}
1、if标签和where标签连用
where标签自动生成where语句,并且where标签可以自动去掉sql语句的第一个多余的“and”
if标签::当if标签传入的是pojo类型,或者包装类型,test里面就直接写传入类型里面的属性名,但是当传入的parameterType是基本数据类型,例如string,如要判断string类型的内容,则需要在test里面判断:_parameter
①不用where标签的做法:
select * from user where 1=1
test="user.sex!=null and user.sex!=''">
and sex=#{user.sex}
test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
@Test
public void fun2() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setSex(null);
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = userMapper.findUserWithQueryVo(queryVo);
System.out.println(list);
}
②用where标签的做法:
select * from user
test="user.sex!=null and user.sex!=''">
and sex=#{user.sex}
test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
java程序和上面一样
2、动态更新语句:
3、sql片段
把相同的sql片段抽取出去
标签定义片段;; 来引入sql片段 id和refid相对应
namespace="com.itcast.dao.UserMapper" >
id="where_if_vo">
test="user.sex!=null and user.sex!=''">
and sex=#{user.sex}
test="user.username!=null and user.username!=''">
and username like "%"#{user.username}"%"
select * from user
refid="where_if_vo">
select count(2) from user
refid="where_if_vo">
4、foreach动态遍历集合参数
①第一种方法:
select * from user where (id=22 or id=23 or id=33)
select * from user
collection="ids" item="id" open="(" separator="or" close=")" >
id=#{id}
@Test
public void fun4() throws IOException{
InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo = new QueryVo();
List<Integer> ids = new ArrayList<>();
ids.add(22);
ids.add(24);
ids.add(26);
queryVo.setIds(ids);
List<User> list = userMapper.findUserWithOr(queryVo);
System.out.println(list);
}
②另一种方法:
select * from user where id in (22,23,33)
select * from user
collection="ids" item="id" open="id IN(" separator="," close=")" >
#{id}
java代码只要改个方法名即可
arraylist、list=======》list
数组====》array
spring配置文件:
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
location="classpath:jdbc.properties" />
id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
name="driverClassName" value="${jdbc.driver}">
name="url" value="${jdbc.url}">
name="username" value="${jdbc.username}">
name="password" value="${jdbc.password}">
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
name="dataSource" ref="dataSource" >
name="typeAliasesPackage" value="com.itcast.domain" >
name="configLocation" value="classpath:sqlMapConfig.xml" >
class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
name="basePackage" value="com.itcast.dao" >
另一种扫描接口的方法:
或者直接在sqlMapConfig.xml里面配置
1、传统模式整合:
applicationContext.xml配置
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
location="classpath:jdbc.properties" />
id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" >
name="driverClassName" value="${jdbc.driver}">
name="url" value="${jdbc.url}">
name="username" value="${jdbc.username}">
name="password" value="${jdbc.password}">
id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
name="dataSource" ref="dataSource" >
name="typeAliasesPackage" value="com.itcast.domain" >
name="configLocation" value="classpath:sqlMapConfig.xml" >
class="com.itcast.dao.impl.UserMapperImpl" >
name="sqlSessionFactory" ref="sqlSessionFactory" >
sqlMapConfig.xml配置:
resource="com/itcast/dao/UserMapper.xml" />
使用传统模式,有实现类
public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> findUserWithQueryVo(QueryVo queryVo) {
List<User> list = this.getSqlSession().selectList("com.itcast.dao.UserMapper.findUserWithQueryVo", queryVo);
return list;
}
}
测试类:
@Test
public void fun2() throws IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserMapperImpl mapperImpl = context.getBean(UserMapperImpl.class);
User user = new User();
user.setSex("男");
user.setUsername("张");
QueryVo queryVo = new QueryVo();
queryVo.setUser(user);
List<User> list = mapperImpl.findUserWithQueryVo(queryVo);
System.out.println(list);
}
2、接口代理模式整合:
applicationContext.xml加上接口扫描即可
测试:
UserMapper bean = context.getBean(UserMapper.class);