日志级别: debug< info < Warning < error
ch.qos.logback
logback-classic
1.2.3
@Test
public void testLogger(){
Logger logger = LoggerFactory.getLogger(MyBatisTest.class);
logger.debug("我是debug日志");
logger.info("我是info日志");
logger.warn("我是警告日志");
logger.error("我是错误日志");
}
info == System.out
logback日志默认加载resources中的logback.xml
[%d{HH:mm:ss.SSS}] [%-5level] [%thread] [%logger] [%msg]%n
UTF-8
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/top_news?serverTimezone=UTC&rewriteBatchedStatements=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
编辑mybatis的核心配置文件
注意标签位置
别名定义
定义别名包 :指定公共前缀
sql标签
${xxxx}取值
$符取值 没有引号 需要自己手动添加
$符取值 没有预编译的效果 ,有sql注入的风险
$符只是字符串的拼接
#{}取值
#有预编译的效果
#会为字符串自动添加引号
@Test
public void test02() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
String name = "星期一";
List userList = mapper.findName(name);
userList.forEach(System.out::println);
}
模糊查询时使用
@Test
public void test03() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
String name = "节";
List userList = mapper.findLikeName(name);
userList.forEach(System.out::println);
}
以字段为参数时
@Test
public void test04() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
String column = "age";
List userList = mapper.findOrderBy(column);
userList.forEach(System.out::println);
}
原则:
单值传递与名无关,只与下标有关 args,param
1. 使用#号和$
符时,如果只有一个参数时,使用任意名称都可以获取参数
2. 但是使用$
{} 时 不能以数字为参数
3.使用$ 不能写NULL
使用$不能写数字,但是#可以
说明:接口中的参数个数有多个
取值规则:arg0,arg1 param1,param2
@Test
public void test06() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int age = 18;
String sex = "女";
List userList = mapper.findByAS(age,sex);
userList.forEach(System.out::println);
}
说明: 基于mybatis的原则,可以把多值封装为单值对象
@Test
public void test05() {
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int age = 18;
String sex = "女";
Map map = new HashMap();
map.put("age",age);
map.put("sex",sex);
List userList = mapper.findByAS(map);
userList.forEach(System.out::println);
}
mybatis针对Map封装数据提供了注解的支持
List findByAnno(@Param("age") int age,
@Param("sex") String sex);
增删改需要开启事务
增加
@Test
public void testInsert(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(null,"刘谦",30,"男");
int rows= mapper.insertUser(user);
System.out.println("影响的行数" + rows);
}
insert into user values (null,#{name},#{age},#{sex})
修改
@Test
public void testUpdate(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(41,"宋江",30,"男");
int rows = mapper.updateUser(user);
System.out.println("影响的行数"+rows);
}
@Test
public void testUpdate1(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int rows = mapper.updateUser(new User(2,"吴用",10,"男"));
System.out.println("影响的行数"+rows);
}
update user
set name = #{name},
age = #{age},
sex = #{sex}
where id = #{id}
删除
@Test
public void testDelete(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int id = mapper.deleteUser(1);
System.out.println("影响的行数"+id);
}
delete from user where id = #{id}
说明:MyBatis默认条件下使用mappers标签引入映射文件
优化:采用package标签优化属性
注意事项:
mapper.xml映射文件的路径必须与接口的包路径一致 用 / 创建包
target包里面接口和xml在一个包下
@Test
public void test08(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
map.put("minage",18);
map.put("maxage",100);
List userlist =mapper.findByAge(map);
userlist.forEach(System.out::println);
}
CDATA标签:
在sql语句中 当大于或者小于号或者其他符号被当做标签使用,则使用转义字符
有时查询的数据可能只返回部分结果,没有返回全部数据,用map返回
MyBatis针对与类型有两种写法
1. 包名.类型 java.util.Map
2.小写字符 map (要配置别名包)
@Test
public void test09(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
String sex = "男";
List userlist =mapper.findBySex(sex);
System.out.println(userlist);
}
useGeneratedKeys = "true" 开启主键自增
keyColumn = "id" 指定自增的字段 主键自增 一张表只有一个 可以不写
keyProperty= "id" 制定映射的属性
@Test
public void testInsert(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(null,"刘谦",30,"男");
// int rows= mapper.insertUser(user);
// System.out.println("影响的行数" + rows);
mapper.insertUser(user);
System.out.println(user);
}
insert into user
values (null, #{name}, #{age}, #{sex})
1.foreach循环遍历
@Test
public void test10(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Integer[] array = {1,3,4,5,7};
List list = Arrays.asList(array);//利用get方法获取参数
List userList = mapper.getArray(list);
System.out.println(userList);
}
如果循环遍历时,参数是Map集合,则collection 写map中的key 也就是数组的名称
要求:map.value是一个可遍历的值 例如是一个list,array
//要求: 查询id= 1,3,4,5,同时sex = "男"
@Test
public void test11(){
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
map.put("ids",new Integer[]{1,3,4,5});
map.put("sex","男");
List userList = mapper.selectArray(map);
System.out.println(userList);
}