自学Mybatis的回顾(1/1)
Mybatis概述
Mybatis是整合了JDBC过程的映射,满足对数据库操作的所有需求。
搭建Mybatis框架
mybatis的实现步骤:
1、新建student数据库表
2、加入maven的mybatis坐标,mysql的驱动坐标
3、创建实体类student类
4、创建持久层的dao接口,定义操作数据库的方法
5、创建一个mybatis使用的配置文件,这个文件是xml文件
6、创建mybatis的主配置文件
7、创建使用mybatis类,访问数据库
具体的实现步骤:
1、首先搭建maven框架,选用的是quickstart。
2、配置mysql-connector-java和Mybatis的jar包到pom.xml文件的依赖中
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
3、在pom.xml文件的bulid中,添加resources标签来将除java文件的资源文件一起拷贝到target目录中。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
4、创建Student实体类,属性名尽量和字段名一致,方便开发,get和set方法要有
public class Student {
private Integer no;
private String name;
private String email;
private Integer age;
public Integer getNo() {
return no;
}
public void setNo(Integer no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(Integer no, String name, String email, Integer age) {
this.no = no;
this.name = name;
this.email = email;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
5、创建dao接口,接口中定义各种对数据库增删改查的抽象方法、
public interface StudentDao {
public List<Student> select();
}
6、创建xml配置文件
<?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="StudentDao">
<select id="select" resultType="Student">
select * from student
</select>
</mapper>
7、创建xml主配置文件,用来配置与jdbc连接相关的配置信息。(在resources资源文件夹下创建,为了方便管理,在同一目录下再创建一个jdbc.properties配置文件)
<?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>
<properties resource="jdbc.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentDao.xml"/>
</mappers>
</configuration>
8、创建mybtis工具类并使用此工具类对数据库对数据操作
public class MyBatisUtil {
private static SqlSessionFactory build;
static {
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
build = sqlSessionFactoryBuilder.build(in);
}
public static SqlSession getSqlSession(){
SqlSession sqlSession=null;
if(sqlSession==null){
sqlSession=build.openSession();
}
return sqlSession;
}
}
在接口现实类中主要对sqlSession处理
public class StudentDaoImpl implements StudentDao{
@Override
public List<Student> select() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
List<Student> objects = sqlSession.selectList("StudentDao.select");
sqlSession.close();
return objects;
}
}
9、最后测试
public class Test {
public static void main(String[] args) throws IOException {
StudentDao studentDao=new StudentDaoImpl();
List<Student> select = studentDao.select();
for(Student s:select){
System.out.println(s);
}
}
}
结果:
dao层的动态代理
由于每次都要写dao接口的实现类实现方法会很繁琐,mybatis提供了方法实现的动态代理,就可以省略impl实现类。
条件:
1、xml配置文件名和dao接口文件名一致。
2、xml配置文件中的mapper标签的namespace全限定名要和dao接口文件名一致。
3、mapper标签中的select、insert等标签id属性值和dao接口文件中抽象方法一致。
使用:
1、使用sqlSession的getMapper方法来得到dao实现类。
public void testSelect() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
List<Student> select = mapper.select();
for (Student student : select) {
System.out.println(student);
}
sqlSession.close();
}
Mybatis传参数的方式
1、一个简单类型的参数:#{}
2、多个简单类型的参数:@ Param(自定义名称)
3、java对象作为参数:#{java对象属性名}
4、根据参数位置:#{arg1}
5、使用Map:#{key}
Student selectById(@Param("myno") Integer i);
select * from student where no=#{myno}
Mybatis中$和#的区别
$相当于Statement,使用字符串拼接的方式,不能防止sql注入现象。
#相当于preparedStatement,使用占位符的方式,能防止sql注入现象,而且效率更高。
Mybatis之动态sql
动态sql就是sql语句能随着java语句的变化而变化。
1、if标签:test属性:判断。如果为true,就将if标签中的添加到sql语句后
2、where标签:where标签一般是处理其中的if标签中多余的or或者and等sql语句
3、foreach标签:方便数组和list的循环。
collection属性:遍历的数据类型
item属性:元素的变量名
open属性:循环开始的字符,一般是(
close属性:循环结束的字符,一般是)
separtor属性:元素分割的字符,一般是,
4、sql代码片段:替换重复的sql语句,提高开发效率。
Mybatis之分页查询工具pagehelper
1、在pom.xml添加依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2、在主配置xml文件的configuration标签下添加
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
</plugins>
3、在test方法中使用
@Test
public void testSelect() {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
PageHelper.startPage(2,3);
List<Student> select = mapper.select();
for (Student student : select) {
System.out.println(student);
}
sqlSession.close();
}