mybatis小技巧 11.28

目录

1.#{}和${}区别(#{}占位符‘XXX’ ${}拼接XXX)

2.什么时候用${}(升序降序 拼接XXX)

3.拼接表名(log_${date})

4.批量删除(or/in(x,x,x)/in($(x))

5.模糊查询(#{brand} |'%${brand}%')

6.别名机制(typeAlias/package)

7.mapper配置(package)

8.配置模板文件

9.自动生成主键值(useGenerateKeys/keyProperty)


1.#{}和${}区别(#{}占位符‘XXX’ ${}拼接XXX)

#{}是占位符: preparestatement 先预编译,后插入值(?占位符)  传入  'xxx'

${}是字符串:statement 先sql语句拼接,后进行编译(存在数据注入问题) 拼接xxx

出错一:不要在mapper.xml配置的sql语句里面注释会报错!!!

    

 测试:

    @org.junit.Test
    public void TestSelectByCarType() {
            SqlSession sqlSession = MybatisTool.getSession();
            CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
            List t_carList = carMapper.SelectByCarType("燃油车");
            for (T_Car t_car : t_carList) {
                System.out.println(t_car);
            }
            sqlSession.commit();
            MybatisTool.close(sqlSession);
    }

 为#{}时

 

结果:

 Preparing: select id,car_num as num,brand,guide_price,produce_time,car_type from t_car where car_type =? 
==> Parameters: 燃油车(String)

Created connection 1073763441.
Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@40005471]
==>  Preparing: select id,car_num as num,brand,guide_price,produce_time,car_type from t_car where car_type =? 
==> Parameters: 燃油车(String)
<==      Total: 12
T_Car{id=5, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=6, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=7, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=12, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=13, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=14, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=15, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=16, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=17, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=32, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=38, num='9991', brand='凯美瑞', guide_price='30.0', produce_time='2020-11-11', car_type='燃油车'}
T_Car{id=39, num='9991', brand='凯美瑞', guide_price='30.0', produce_time='2020-11-11', car_type='燃油车'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@40005471]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@40005471]
Returned connection 1073763441 to pool.

Process finished with exit code 0

 为${}时

报错

 SQL: select id,car_num as num,brand,guide_price,produce_time,car_type from t_car where car_type =燃油车
### Cause: java.sql.SQLSyntaxErrorException: Unknown column '燃油车' in 'where clause'

2.什么时候用${}(升序降序 拼接XXX)

升序降序(desc/asc)

当为#{}时

  

 测试

   @org.junit.Test
    //测试${}
    public void TestSelectAll() {
        SqlSession session = MybatisTool.getSession();
        List cars = session.getMapper(CarMapper.class).SelectaLL("desc");
        cars.forEach(car -> System.out.println(car));
        session.close();
    }

 报错

Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@40005471]
==>  Preparing: select id,car_num as num,brand,guide_price,produce_time,car_type from t_car order by produce_time ? 
==> Parameters: desc(String)

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''desc'' at line 1
### The error may exist in Mapper.xml

 当为${}时

Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@69453e37]
==>  Preparing: select id,car_num as num,brand,guide_price,produce_time,car_type from t_car order by produce_time desc 
==> Parameters: 
<==      Total: 29
T_Car{id=5, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=6, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=7, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=12, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=13, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=14, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=15, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=16, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=17, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=32, num='1004', brand='丰田', guide_price='30.0', produce_time='2020-11-2', car_type='燃油车'}
T_Car{id=38, num='9991', brand='凯美瑞', guide_price='30.0', produce_time='2020-11-11', car_type='燃油车'}
T_Car{id=39, num='9991', brand='凯美瑞', guide_price='30.0', produce_time='2020-11-11', car_type='燃油车'}
T_Car{id=18, num='1005', brand='迈凯轮', guide_price='10.0', produce_time='2020-10-4', car_type='燃油机'}
T_Car{id=19, num='1005', brand='迈凯轮', guide_price='10.0', produce_time='2020-10-4', car_type='燃油机'}
T_Car{id=21, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=22, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=23, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=24, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=25, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=26, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=27, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=28, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=29, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=30, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=31, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=33, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=34, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=35, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
T_Car{id=36, num='111', brand='宝马', guide_price='30.0', produce_time='2020-10-1', car_type='小飞棍来咯!'}
Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@69453e37]
Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@69453e37]

 不知道为什么这里运行不出来 mybatis的版本太低 引用3.5.4就可以了

3.拼接表名(log_${date})

使用${}  传值(“XXX”)  呈现   select * from log_XXX


    

 测试(以时间日期命名的多个log文件,找某个时期的文件)


public class TestLog {
    @Test
    public void  TestLog(){
        SqlSession sqlSession= MybatisTool.getSession();
        LogMapper logMapper=sqlSession.getMapper(LogMapper.class);
        List logs = logMapper.SelectByDate("20221122");
        for (Log log : logs) {
            System.out.println(log);
        }
        sqlSession.commit();
        sqlSession.close();
    }
}

4.批量删除(or/in(x,x,x)/in($(x))

三种方式:

  • delete from t_car where id=1 or id=2 or id=3
  • delete from t_car where id in(1,2,3) 删除id为1,2,3的
  • delete from t_car where id in(${range})
    
   delete from t_car where id in(1,2,3)
    
    
            delete from t_car where id in(${range})
    
    
public class Test03delbulk {
    @Test
    public void testdeletelot(){
        SqlSession sqlSession= MybatisTool.getSession();
        CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
        carMapper.DeleteBulk();
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    //使用${}
    public void TestDeleteBulk(){
        SqlSession sqlSession= MybatisTool.getSession();
        CarMapper carMapper=sqlSession.getMapper(CarMapper.class);
        carMapper.DeleteBulk2("9,10,11");
        sqlSession.commit();
        sqlSession.close();
    }
}

5.模糊查询(#{brand} |'%${brand}%')

出错点: '?'  当占位符处在单引号之间时,将会失去占位功能

普通的查询语句:

  select id,car_num as num,brand,guide_price,produce_time,car_type from t_car where brand like '%丰田%'

两种方式如下:

 #{brand}   传入参数得为 carMapper.Select_LikeByBrand("%丰田%");  放入sql中为 '%丰田%'
'%${which}%' 传入参数为 carMapper.Select_LikeByBrand("丰田")  放入sql中为   '%丰田%'
   
    @Test
    //用${}
    public void TestLike2(){
        SqlSession sqlSession = MybatisTool.getSession();
        CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
        List t_carList = carMapper.Select_LikeByBrand("丰田");
        for (T_Car t_car : t_carList) {
            System.out.println(t_car);
        }
        sqlSession.commit();
        MybatisTool.close(sqlSession);
    }

6.别名机制(typeAlias/package)