朋友 万事开头难 生活的苦和学习的苦 你怎么选择 诗与远方就在奋斗的路上 加油 我们现在的付出终会迎来灿烂的明天
第一节
1.1什么是MyBatis
? MyBatis本是apache的一个开源项目iBatis,2010年因为迁移,改名为MyBatis,一款优秀的持久层框架 ? 支持定制化SQL、存储过程以及高级映射 ? MyBatis避免了几乎所有的JDBC代码和手动设置的参数以及获取结果集 ? MyBatis可以使用简单的XML或注解来配置和映射原生类、接口和java的POJO(Plain Old java Objects,普通老式对象 )为数据库中的记录
1.2获取MyBatis
? 从maven仓库获取
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
1.3什么是持久化
? 就是将程序的数据在持久态和瞬时态转化的过程 ? 为了对象不丢失,所以要持久化
1.4什么是持久层
? Dao层、Service层、Controller.
1.5为什么需要MyBatis
? 方便,帮助我们将数据存入数据库、简单易学、sql和代码分离,提高了可维护性 ? 传统的JDBC代码太复杂
第二节
2.1MyBatis入门使用
2.1.1建立数据库
create database mybatis;
use mybatis;
create table MyBatis(
`id` int(20) not null,
`name` varchar(32) default null,
`pwd` varchar(32) default null)engine = innodb default charset = utf8;
insert into `MyBatis` (`id`,`name`,`pwd`) values
(1,'898717454','898717454'),
(2,'209053771','209053771'),
(3,'13140662112','13140662112'),
(4,'2251231008','2251231008'),
(5,'13526260059','13526260059');
select * from Mybatis;
2.1.2创建一个普通的maven项目
2.1.2删除src
- 这里删除src后去配置pom文件这个pom文件作为父模块中的pom文件,这样的好处是子项目不用每次都配置pom文件了
<?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>com.polarBear</groupId>
<artifactId>MyBatis-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-Demo01</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-RC1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.1.3导入maven依赖
- mysql驱动、MyBatis依赖、junit依赖
<?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>com.polarBear</groupId>
<artifactId>MyBatis-Study</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-RC1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.1.4创建一个模块
2.1.4.1编写Mybatis的核心配置文件
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123abcABC"/>
</dataSource>
</environment>
</environments>
</configuration>
2.1.4.2编写Mybatis工具类
package com.polarbear.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
2.1.5编写代码
? 可能遇到的问题
- 配置文件没有注册-在mybatis核心配置中
- 绑定接口错误
- 方法名不对
- 返回类型不对
- Maven导出资源问题-在pom文件中
2.1.5.1实体类
package com.polarbear.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
2.1.5.2Dao接口
package com.polarbear.dao;
import com.polarbear.pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
2.1.5.3接口实现类
<?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.polarbear.dao.UserDao">
<select id="getUserList" resultType="com.polarbear.pojo.User">
select * from mybatis.user
</select>
</mapper>
2.1.5.4测试
2.1.5.4.1Mybatis核心文件中注册mappers
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSl=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123abcABC"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/polarbear/dao/UserMapper.xml"/>
</mappers>
</configuration>
2.1.5.4.2在pom文件中配置resources
<?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>com.polarBear</groupId>
<artifactId>MyBatis-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-Demo01</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0-RC1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2.1.5.4.3测试代码
package com.polarbear.dao;
import com.polarbear.pojo.User;
import com.polarbear.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user : userList){
System.out.println(user);
}
sqlSession.close();
}
}
|