参考官网:入门_MyBatis中文网
-
创建一个空的maven项目
-
在pom.xml文件添加mybatis,mysql,junit等依赖
org.mybatis mybatis 3.5.5 mysql mysql-connector-java 5.1.32 junit junit 4.13 test -
在IDEA中安装MybatisX插件
-
创建mybatis配置文件:mybatis-config.xml,并且放入resource目录
-
创建pojo包,创建User类并且放入
-
使用mapper进行代理开发
1.在resource文件夹内创建和pojo包前缀相同的目录,如(com.xzh.pojo包),目录为comxzh 2.在resource文件夹创建comxzhmapper目录,创建UserMapper.xml,存放sql语句
3.在java文件夹创建com.xzh.mapper包,创建同名的UserMapper接口 public interface UserMapper { Listselect * from tb_user; selectAll(); } 4.配置好后,对应接口和sql语句位置会出现蓝红鸟对应(mybatisx插件功能),这样就配置好了 -
在测试类里面创建com.xzh.test包,创建对应测试类。
@Test public void testSelectAll() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); Listbrands = brandMapper.selectAll(); System.out.println(brands); sqlSession.close(); } -
如果数据库字段和pojo里面定义类的字段不一致,需要用resultMap进行封装
-
编写接口方法,然后按alt+enter会自动跳转到sql语句文件(mybatisx插件的功能)
List
selectAll(); -
在配置文件内编写sql语句,如果数据库字段和pojo里面定义类的字段不一致,需要用resultMap进行封装
-
执行方法,测试
sql映射文件:
参数占位符
- #{}:执行SQL时,会将#{}占位符替换为? 将来自动设置参数值
- ${}:拼SQL。会存在SQL注入问题
使用时机:
- 参数传递,都使用#{}
- 如果要对表名、列名进行动态设置,只能使用${}进行sql拼接
在xml内如果要输入小于符合会引起冲突,因此使用以下方法解决:
- 转义字符,使用html内的特殊字符
- :输入CD有提示(常用)
sql映射文件
多参数设置的方法
-
散装参数:需要用@Param(“SQL中的参数名称”) *使用麻烦
List
selectByCondition(@Param("status") int status, @Param("companyName") String companyName, @Param("brandName") String brandName); -
实体类封装参数 *只需要保证SQL中的参数名和实体类属性名对应上就行
List
selectByCondition(Brand brand); @Test public void testSelectByCondition() throws IOException { int status = 1; String companyName = "华为"; String brandName = "华为"; //模糊查询需要用% companyName = "%" + companyName + "%"; brandName = "%" + brandName + "%"; Brand brand = new Brand(); brand.setStatus(status); brand.setCompanyName(companyName); brand.setBrandName(brandName); String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); Listbrands = brandMapper.selectByCondition(brand); System.out.println(brands); sqlSession.close(); } -
map集合 *只需要保证SQL中的参数名和map集合的键对的上即可
List
selectByCondition(Map map); @Test public void testSelectByCondition() throws IOException { int status = 1; String companyName = "华为"; String brandName = "华为"; companyName = "%" + companyName + "%"; brandName = "%" + brandName + "%"; Mapmap = new HashMap<>(); map.put("status", status + ""); map.put("companyName", companyName); map.put("brandName", brandName); String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class); List brands = brandMapper.selectByCondition(map); System.out.println(brands); sqlSession.close(); }
以下语句如果用户只给了brandName的值 那么语句就会出错(where and brand_name like #{brandName})
解决方案:
-
使用恒等式让所有条件格式都一样(where 1=1) *旧方案
-
标签替换 where 关键字
1.编写Mapper接口方法
void add(Brand brand);
2.编写SQL语句:SQL映射文件
insert into tb_brand(brand_name, company_name, ordered, description, status) VALUES(#{brandName},#{companyName},#{ordered},#{description},#{status});
3.编写测试用例
注意:mybatis默认关闭自动提交,如果需要添加语句生效,需要手动提交事务(sqlSession.commit();)或者设置为自动提交事务(openSession(true))
主键返回当添加操作完成后,返回主键id值以便于后续关联操作
返回的id值会封装到传入的Brand类内
insert into tb_brand(brand_name, company_name, ordered, description, status) VALUES (#{brandName}, #{companyName}, #{ordered}, #{description}, #{status});
@Test
public void testAdd() throws IOException {
String brandName = "飒飒是";
String companyName = "电视动画";
int ordered = 10;
String description = "sdsdssds";
int status = 1;
Brand brand = new Brand();
brand.setStatus(status);
brand.setCompanyName(companyName);
brand.setBrandName(brandName);
brand.setDescription(description);
brand.setOrdered(ordered);
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
brandMapper.add(brand);
System.out.println("add done +brand.getId());
sqlSession.commit();
sqlSession.close();
}
修改-动态修改
使用配合完成动态修改,因为不用动态set的话,如果只修改单个参数,那么sql语句就会出错(会多出逗号)
删除-批量删除update tb_brand where id = #{id}; brand_name = #{brandName}, company_name = #{companyName}, ordered = #{ordered}, description = #{description}, status = #{status},
接口写法:使用注解把主键名称改成ids,以便于后续调用
void deleteByIds(@Param("ids") int[] ids);
SQL:使用遍历数据,并且将数据动态拼接
Mybatis底层参数封装delete from tb_brand where id in #{id}
*单个参数
-
pojo类:直接使用,只需让 属性名 和 占位参数名称 一致。
-
Map集合:直接使用,只需让 键名 和 占位参数名称 一致。
-
Collection:封装为Map集合
map.put(“arg0”,collection集合);
map.put(“collection”,collection集合);
-
List:
map.put(“arg0”,list集合);
map.put(“collection”,list集合);
map.put(“list”,list集合);
-
Array:
map.put(“arg0”,array数组);
map.put(“array”,array数组);
-
其他类型:int char 等 ,直接使用
*多个参数:封装为Map集合,可以使用@Param()注解替换map中默认的arg键名
map.put(“arg0”,参数1);
map.put(“param1”,参数1);
map.put(“arg1”,参数2);
map.put(“param2”,参数2);
-----------使用@Param(“username”) 注解后
map.put(“username”,参数1);
map.put(“param1”,参数1);
map.put(“arg1”,参数2);
map.put(“param2”,参数2);
注解SQL语句提示:注解完成简单sql 配置文件完成复杂sql
@Select(value="select * from tb_user where id=#{id}")
public User select(int id)



