目录
Maven
Maven简介
Maven常用命令
Maven坐标详解
MyBatis
什么是MyBatis?
持久层
框架
MyBatis快速入门
Mapper代理开发
目的:
步骤:
配置文件完成增删改查
查询所有:
查看详情:
查询——多条件查询
查询-多条件-动态条件查询
查询-单条件-动态条件查询
Maven
Maven时专门用于管理和构建Java项目的工具,它的主要功能有:
- 提供了一套标准化的项目结构
- 提供了一套标准化的构建流程(编译,测试,打包,发布)
- 提供了一套依赖管理机制
依赖管理:
管理项目所依赖的第三方资源(jar包、插件……)
Maven简介
Apache Maven 是一个项目管理和构建工具,它基于项目对象模型(POM)的概念,通过一小段描述信息来管理项目的构建、报告和文档
官网:http://maven.apache.org/
仓库分类:
- 本地仓库:自己计算机上的一个目录
- 中央仓库:由Maven团队维护的全球唯一的仓库 https://repo1.maven.org/maven2/
- 远程仓库(私服):一般由公司团队搭建的私有仓库
当项目中使用坐标引入对应依赖jar包后,首先会查找本地仓库中是否有对应地jar包
- 如果有,则在项目直接引用
- 如果没有,则去中央仓库中下载对应地jar包到本地仓库
Maven常用命令
- compile:编译
- clean:清理
- test:测试
- package:打包
- install:安装
Maven坐标详解
什么是坐标?
- Maven中的坐标时资源的唯一标识
- 使用坐标来定义项目或引入项目中需要的依赖
Maven坐标主要组成
- groupId:定义当前Maven项目隶属组织名称(通常是域名反写,例如:com.alibaba)
- artifactId:定义当前Maven项目名称(通常是模块名称,例如order-service,goods-service)
- version:定义当前项目版本号
依赖管理
从IDEA自带的仓库中导入
- 在pom.xml中按alt + insert,选择Dependency
- 在弹出来的面板中搜索对应坐标,然后双击选中对应坐标
- 点击刷新按钮,是坐标生效
如果对上述以来版本不满意的话也可以在网上搜索对应的依赖
依赖范围
通过设置坐标的依赖范围(scope),可以设置对应jar包的作用范围:编译环境、测试环境、运行环境
依赖范围 | 编译classpath | 测试classpath | 运行classpath | 例子 |
compile | Y | Y | Y | druid |
test | - | Y | - | Junit |
provided | Y | Y | - | servlet-api |
MyBatis
什么是MyBatis?
- MyBatis是一款优秀的持久层框架,用于简化JDBC开发
- MyBatis本事Apache的一个开源项目iBatis,2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis。2013年11月迁移到Github
- 官网:https://mybatis.org/mybatis-3/zh/index.html
持久层
- 负责将数据保存到数据库的那一层代码
- JavaEE三层架构:表现层、业务层、持久层
框架
- 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
- 在框架的基础之上构建软甲编写更加搞笑、规范、通用、可扩展
MyBatis快速入门
-
创建user表,添加数据
-
创建模块,导入坐标
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>MyBatis-demo</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.9</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.25</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>1.7.25</version></dependency><!--<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-nop</artifactId><version>1.7.2</version></dependency>--><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency></dependencies></project>
-
编写MyBatis核心配置文件 --> 替换连接信息 解决硬编码问题
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"> <!-- 数据库连接信息--><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis?useSeverPrepStmts=true"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><!--加载sql的映射文件--><mapper resource="UserMapper.xml"/></mappers> </configuration>
-
编写SQL映射文件 --> 统一管理sql语句,解决硬编码问题
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace:名称空间 --> <mapper namespace="com.shenyi.pojo.mapper.UserMapper"><select id="selectAll" resultType="com.shenyi.pojo.User">select * from tb_user;</select> </mapper>
-
编码
-
定义POJO类
public class User {private Integer id;private String username;private String password;private String gender;private String addr;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getAddr() {return addr;}public void setAddr(String addr) {this.addr = addr;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", gender='" + gender + '\'' +", addr='" + addr + '\'' +'}';} }
-
加载核心配置文件,获取SqlSessionFactory对象
-
获取SqlSession对象,执行SQL语句
-
释放资源
-
/*
* Mybatis快速入门
* */
public class MyBatisDemo {public static void main(String[] args) throws IOException {//1.加载mybatis核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象,用他来执行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.执行sqlList<User> users = sqlSession.selectList("test.selectAll");System.out.println(users);//4.释放资源sqlSession.close();}}
Mapper代理开发
目的:
->解决原生方式中的硬编码
->简化后期执行SQL
步骤:
- 定义与SQL映射文件同名的Mapper接口,并将Mapper接口和SQL映射文件放置在同一目录下
import java.util.List;public interface UserMapper {List<User> selectAll(); }
- 设置SQL映射文件的namespace属性为Mapper接口全限定名
- 在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
- 编码
- 通过SqlSession的getMapper方法获取Mapper接口的代理对象
- 调用对应方法完成sql的执行
/*
* Mybatis代理开发
* */
public class MyBatisDemo2 {public static void main(String[] args) throws IOException {//1.加载mybatis核心配置文件,获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象,哟个他来执行sqlSqlSession sqlSession = sqlSessionFactory.openSession();//3.执行sql//List<User> users = sqlSession.selectList("test.selectAll");//3.1 获取UserMapper接口的代理对象UserMapper usermapper = sqlSession.getMapper(UserMapper.class);List<User> users = usermapper.selectAll();System.out.println(users);//4.释放资源sqlSession.close();}}
细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以是用包扫描的方式建花SQL映射文件的加载
<!--Mapper代理方式--> <package name="com.shenyi.pojo.mapper"/>
配置文件完成增删改查
查询所有:
1.编写接口方法:Mapper接口
List<Brand> selectAll();
考虑:
- 需要执行的sql语句
- 参数:不需要
- 结果:List<Brand>
2.编写SQL语句:SQL映射文件:
<!--数据库表的字段名称 和 实体类的属性名称 不一样,则不能自动封装数据* 起别名,对不一样的列名起别名,让别名和实体类的属性名一样就可以了<select id="selectAll" resultType="Brand">select id, brand_name as brandName, company_name as companyName, ordered, scorefrom tb_brand;</select>-->* resultMap1.定义<resultMap>标签2.在<select>标签中,使用resultMap属性替换resultType属性
--><!--resultMap--><!--id:唯一标识type:映射的类型,支持别名--><resultMap id="brandResultMap" type="Brand"><!--id:完成主键字段映射column:列名property:实体类的属性名result:完成一般字段映射column:列名property:实体类的属性名--><result column="brand_name" property="brandName"></result><result column="company_name" property="companyName"></result></resultMap><select id="selectAll" resultMap="brandResultMap">select *from tb_brand;</select>
3.执行方法:测试
@Testpublic void testSelectAll() throws IOException {//1.获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.执行方法List<Brand> brands = brandMapper.selectAll();System.out.println(brands);//5.释放资源sqlSession.close();}
查看详情:
1.编写接口方法:Mapper接口
Brand selectByld(int id);
- 参数:id
- 结果:brand
2.编写SQL语句:SQL映射文件
<!--参数占位符:1.#{}:会将其替换为?,为了防止SQL注入2.${}:之劫贫sql,会存在SQL注入的问题3.使用时机:* 参数传递时:#{}* 表名或列名不固定的情况下:${} 会存在SQL注入的情况* 参数类型:parameterType:可以省略* 特殊字符处理:1.转义字符:<2.CDATA区--><!--<select id="selectById" parameterType="int" resultMap="brandResultMap">select *from tb_brand where id<![CDATA[<]]>#{id};</select>--><select id="selectById" parameterType="int" resultMap="brandResultMap">select *from tb_brand where id = #{id};</select>
3.执行方法:测试
@Testpublic void testSelectById() throws IOException {//接收参数int id = 1;//1.获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.执行方法Brand brand = brandMapper.selectById(id);System.out.println(brand);//5.释放资源sqlSession.close();}
查询——多条件查询
1.编写接口方法:Mapper接口(散装参数)
/* 1.散装参数:如果方法中有多个参数,需要使用@Param("参数占位符名称") */
List<Brand> selectByCondition(@Param("score") int score, @Param("companyName") String companyName, @Param("brandName") String brandName);
}
2.编写SQL语句:SQL映射文件
<select id="selectByCondition" resultMap="brandResultMap">select *from tb_brandwhere score > #{score}or company_name like #{companyName}or brand_name like #{brandName};
</select>
3.执行方法:测试
@Testpublic void testSelectByCondition() throws IOException {//接收参数int score = 70;String companyName = "育碧";String brandName = "theft";//处理参数companyName ="%" + companyName + "%";brandName ="%" + brandName + "%";//1.获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.执行方法List<Brand> brands = brandMapper.selectByCondition(score, companyName, brandName);System.out.println(brands);//5.释放资源sqlSession.close();}
1.编写接口方法:Mapper接口(实体类封装参数)
List<Brand> selectByCondition(Brand brand);
2.编写SQL语句:SQL映射文件
<select id="selectByCondition" resultMap="brandResultMap">select *from tb_brandwhere score > #{score}or company_name like #{companyName}or brand_name like #{brandName};</select>
3.执行方法:测试
1.编写接口方法:Mapper接口(Map集合)
List<Brand> selectByCondition(Map map);
2.编写SQL语句:SQL映射文件(同上)
3.执行方法:测试
@Testpublic void testSelectByCondition() throws IOException {//接收参数int score = 70;String companyName = "育碧";String brandName = "theft";//处理参数companyName ="%" + companyName + "%";brandName ="%" + brandName + "%";//封装对象/*Brand brand = new Brand();brand.setScore(score);brand.setCompanyName(companyName);brand.setBrandName(brandName);*/Map map = new HashMap();map.put("score",score);map.put("companyName",companyName);map.put("brandName",brandName);//1.获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.执行方法//List<Brand> brands = brandMapper.selectByCondition(score, companyName, brandName);//List<Brand> brands = brandMapper.selectByCondition(brand);List<Brand> brands = brandMapper.selectByCondition(map);System.out.println(brands);//5.释放资源sqlSession.close();}
缺点是用户必须输入所有条件,如果缺少条件将不能进行查询
查询-多条件-动态条件查询
SQL语句会随着用户的输入或外部条件的变化而变化,我们成为动态SQL
<select id="selectByCondition" resultMap="brandResultMap">select *from tb_brand# where<where><if test="score != null">score > #{score}</if><if test="companyName != null and companyName != ''">or company_name like #{companyName}</if><if test="brandName != null and companyName != ''">or brand_name like #{brandName};</if></where></select>
Map map = new HashMap();//map.put("score",score);map.put("companyName",companyName);map.put("brandName",brandName);
这样在用户不输入score的情况下也能完成查询
查询-单条件-动态条件查询
- 从多个条件中选一个
- choose(when,otherwise):选择,类似于Java中的swich(case,default)语句
<!--单条件查询--><select id="selectByConditionSingle" resultMap="brandResultMap">select *from tb_brand<where><choose> <!--相当于switch--><when test="score != null"><!--相当于case-->score > #{score}</when><when test="companyName != null and companyName != ''"><!--相当于case-->company_name like #{companyName}</when><when test="brandName != null and companyName != ''"><!--相当于case-->brand_name like #{brandName};</when><!--<otherwise>1 = 1</otherwise>--></choose></where></select>
测试:
@Testpublic void testSelectByConditionSingle() throws IOException {//接收参数int score = 70;String companyName = "育碧";String brandName = "theft";//处理参数companyName ="%" + companyName + "%";brandName ="%" + brandName + "%";//封装对象Brand brand = new Brand();
// brand.setScore(score);
// brand.setCompanyName(companyName);
// brand.setBrandName(brandName);//1.获取SqlSessionFactoryString resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//2.获取SqlSession对象SqlSession sqlSession = sqlSessionFactory.openSession();//3.获取Mapper接口的代理对象BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);//4.执行方法//List<Brand> brands = brandMapper.selectByCondition(score, companyName, brandName);//List<Brand> brands = brandMapper.selectByCondition(brand);List<Brand> brands = brandMapper.selectByConditionSingle(brand);System.out.println(brands);//5.释放资源sqlSession.close();}