配置typeAliases,为 com.terwergreen.pojo.User 指定别名 user
上面是我们自定义的别名,Mybatis 默认已经为我们设置好了一些别名:
如果实体类较多,上面方法不可取,可以使用指定包名的方式
批量起别名:该包下所有类本身的类名,不区分大小写
mapper.xml
1)动态SQL
动态SQL语句概述
https://mybatis.org/mybatis-3/dynamic-sql.html
动态SQL之
根据实体类的不用取值,使用不同的SQL进行查询
动态SQL之 where
同时设置id和username之后,日志如下:
/** * where测试 * * @throws IOException */ @Test @Throws(IOException::class) fun test7() { val resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml") val sqlSessiOnFactory= SqlSessionFactoryBuilder().build(resourceAsStream) val sqlSession = sqlSessionFactory.openSession() val userDao = sqlSession.getMapper(IUserDao::class.java) val user = User() user.id = 1 user.username = "tyw" val userList = userDao.findByConditionWhere(user) for (user2 in userList) { println(user2) } }
public void test7() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessiOnFactory= new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); User user = new User(); user.setId(1); user.setUsername("tyw"); List userList = userDao.findByConditionWhere(user); for (User user2 : userList) { System.out.println(user2); } }
==> Preparing: select * from user WHERE id=? and username=? ==> Parameters: 1(Integer), tyw(String) <== Columns: id, username <== Row: 1, tyw <== Total: 1 User{id=1, username='tyw'}
动态SQL之 foreach
循环执行SQL的拼接动作。例如:select * from user where id in(1,2,4)
测试代码
/** * foreach测试 * * @throws IOException */ @Test @Throws(IOException::class) fun test8() { val resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml") val sqlSessiOnFactory= SqlSessionFactoryBuilder().build(resourceAsStream) val sqlSession = sqlSessionFactory.openSession() val userDao = sqlSession.getMapper(IUserDao::class.java) val ids = arrayOf(1, 2, 4) val userList = userDao.findByIds(ids) for (user2 in userList) { println(user2) } }
public void test8() throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); SqlSessionFactory sqlSessiOnFactory= new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession(); IUserDao userDao = sqlSession.getMapper(IUserDao.class); Integer[] ids = new Integer[]{1, 2, 4}; List userList = userDao.findByIds(ids); for (User user2 : userList) { System.out.println(user2); } }
配置如下
日志如下:
==> Preparing: select * from user WHERE id in ( ? , ? , ? ) ==> Parameters: 1(Integer), 2(Integer), 4(Integer) <== Columns: id, username <== Row: 1, tyw <== Row: 2, 张月 <== Row: 4, haha <== Total: 3 User{id=1, username='tyw'} User{id=2, username='张月'} User{id=4, username='haha'}
foreach各项的含义如下:
用于遍历几乎
collection:要遍历的集合元素,不能带有#{}
open:语句开始部分
close:结束部分
item:遍历的元素生成的变量名
sperator:分隔符
SQL片段抽取
可以将重复sql抽取出来放在sql标签中,使用时候用include
select * from user
使用
测试:
/** * sql抽取测试 * * @throws IOException */ @Test @Throws(IOException::class) fun test9() { val resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml") val sqlSessiOnFactory= SqlSessionFactoryBuilder().build(resourceAsStream) val sqlSession = sqlSessionFactory.openSession() val userDao = sqlSession.getMapper(IUserDao::class.java) val user2 = userDao.findById(1) println(user2) }