目录
1.创建数据库
2.导入Mybatis的依赖
3.Mybatis核心配置文件
4.创建Mybatis工具类
5、创建实体类
6、编写Mapper接口类
7、编写Mapper.xml配置文件
8、编写测试类
1.数据库创建
CREATE DATABASE `mybatis`;
USE `mybatis`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` INT(20) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES (1,'chen','sdgasjkdg'),(2,'wang','888888'),(3,'li','5645645');
2.导入依赖
<?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>com.chang.Mybatis</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-01</module>
</modules>
<dependencies>
<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>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.核心配置文件
<?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">
<!--core configuration file-->
<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/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
4.工具类
//工具类的目的: 将资源加载进来 并且创建一个可以执行sql的对象(sqlsession)
package com.chang.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.IOException;
import java.io.InputStream;
//工具类的目的: 将资源加载进来 并且创建一个可以执行sql的对象(sqlsession)
public class MybatisUtils {
//提升sqlsessionFactory的作用域
private static SqlSessionFactory sqlSessionFactory;
//使用Mybatis先要构建工具类 :从 XML 中构建 SqlSessionFactory
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//有了SqlSessionFactory 就要获取sqlsession
//获取SqlSession连接 sqlsession就是操作数据库的对象了,里面有很多操作数据库的方法
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
5.实体类
package com.chang.pojo;
public class User {
private int id; //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 + '\'' +
'}';
}
}
6.Mapper接口
Mapper接口相当于Userdao接口
package com.chang.dao;
import com.chang.pojo.User;
import java.util.List;
public interface UserDao {
List<User> getUserList();
}
7.Mapper.xml文件
相当于是Mapper接口的实现类
<?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">
<!--这个xml文件就相当于dao接口的实现类-->
<!--namespace绑定一个对应的dao\mapper接口-->
<mapper namespace="com.chang.dao.UserDao">
<!--相当于实现了UserDao接口-->
<!-- “<” 符号下有提示很多种sql方法 这里用选择(select)语句来举例-->
<select id="selectUser" resultType="com.chang.pojo.User">
select * from user
</select>
</mapper>
注意命名空间Namespace是Mapper(UserDao)接中的方法名
结果类型 resultType是实体类User? 也就是说对应数据库中的一张User表。
8.测试类
package com.chang.dao;
import com.chang.pojo.User;
import com.chang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.graalvm.compiler.lir.LIRInstruction;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
//测试类第一步:获得sqlsession对象 通过刚才创建的MybatisUtils的工具类中的getSession()方法获得sqlSession对象
SqlSession sqlSession = MybatisUtils.getSession();
//第二步 通过反射机制获得接口UserDao接口中的方法 执行sql
//方法一 getMapper
UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.getUserList();
for (User user: userList){
System.out.println(user);
}
sqlSession.close();
}
}
|