一. MyBatis介绍
Mybatis就是一个封装jdbc的持久层框架,它和hibernate都属于ORM框架,但是具体的说,hibernate是一个完全的orm框架,而mbatis是一个不完全的orm框架.
Mybatis让程序员只关注sql本身,而不需要去关注如连接的创建、statement 的创建等操作。
Mybatis会将输入参数据、输出结果进行映射
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis,实质上Mybatis对ibatis进行一些改进。
MyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注 SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
Mybatis通过xml或注解的方式将要执行的各种statement(statement、preparedStatemnt、CallableStatement)配置起来,并通过java对象和statement中的sql进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射成java对象并返回。
整体结构
但凡半句废话就算我输,先把配置文件写好
我们先写一个查询
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
SqlMapConfig.xml-----config代表配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//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.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mysql02?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/gsdx/mapper/computerMapper.xml"/>
</mappers>
</configuration>
computerMapper接口就是管理我们sql语句的地方
id很重要,必须和我们的方法名一样
public interface computerMapper {
//查询所有
public List<Computer> queryAll();
}
namespace对应接口computerMapper接口
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gsdx.mapper.computerMapper">
<select id="queryAll" resultType="com.gsdx.entity.Computer">
select * from computer
</select>
</mapper>
电脑类
@Data
public class Computer {
private Integer id;
private String name;
private String brand;
private double price;
}
测试
public class Test {
@org.junit.Test
public void test(){
try {
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession连接
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行
List<Computer> list = sqlSession.selectList("com.gsdx.mapper.computerMapper.queryAll");
for (Computer computer:list){
System.out.println(computer);
}
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
那好,我们在mapper.xml中,把增删改查写完
先讲一下我们遇到的一个细节
#{}:相当于预处理中的占位符?。
#{}里面的参数表示接收java输入参数的名称。
#{}可以接受HashMap、简单类型、POJO类型的参数。
当接受简单类型的参数时,#{}里面可以是value,也可以是其他。
#{}可以防止SQL注入。
${}:相当于拼接SQL串,对传入的值不做任何解释的原样输出。
${}会引起SQL注入,所以要谨慎使用。
${}可以接受HashMap、简单类型、POJO类型的参数。
当接受简单类型的参数时,${}里面只能是value。
接下来先看我的mapper接口
接下来是xml文件的sql语句
接下来是测试,一种是通过包去调用方法,另一种是万能map,需要通过反射获取
根据id查询
根据名字,品牌查询
模糊查询
删除
添加
修改,先查出来再修改
好了,这期先结束了