首先建立Maven工程
一、配置Maven配置文件 pom.xml
<?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</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>MyBatis-01</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 使用mybatis需要添加的依赖 mybatis 、mysql、junit-->
<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>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--在build里配置resource,防止我们资源配置失败-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
二、Mybatis 核心 配置文件 Mybatis-config.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 使用mybatis第二步,编写核心配置文件,环境准备工作-->
<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?userSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- 每一Mapper.xml都需要在mybatis核心配置文件中注册
这个mapper关联着UserMapper.xml里的mapper namespace路径 -->
<mappers>
<mapper resource="com/pjx/dao/UserMapper.xml"/>
</mappers>
</configuration>
<!-- note:
environments : 环境集
mapper : 映射器
transactionManager:事务管理
dataSource:数据源 property: 属性
-->
三、建立Mybatis工具类 MybatisUtils
package com.pjx.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;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//使用Mybatis第一步、获取sqlsessionfactory对象
String resource = "Mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//获取SqlSession连接
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
四、在pojo创建实体类 User
package com.pjx.pojo;
//使用mybatis第三步/实体类
public class User {
private int id; //id
private String name; //姓名
private String pwd; //密码
//构造,有参,无参
//set/get
//toString()
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 + '\'' +
'}';
}
}
Dao层写实体类的接口UserMapper
package com.pjx.dao;
import com.pjx.pojo.User;
import java.util.List;
//使用mybatis第四步/接口
public interface UserMapper {
List<User> selectUser();
User getUserbyId(int id);
int addUser(User user);
}
五、Dao层写具体实现sql操作的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">
<!-- 使用mybatis第五步/写mapper具体使用数据库的方法-->
<mapper namespace="com.pjx.dao.UserMapper">
<!-- 接口中的定义:List<User> selectUser();
id 相当于 方法名字
resultType 相当于 返回值
parameterType 相当于 参数类型-->
<select id="selectUser" resultType="com.pjx.pojo.User" >
select * from user
</select>
<!-- 这个 #{id} 表示取UserMapper中的 getUserbyid中的参数 id-->
<select id="getUserbyId" resultType="com.pjx.pojo.User" parameterType="int">
select * from user where id= #{id}
</select>
<!-- 对象中的属性可以直接取出来(#{},#{},#{}-->
<insert id="addUser" parameterType="com.pjx.pojo.User" >
insert into user (id,name,pwd) values (#{id},#{name},#{pwd});
</insert>
</mapper>
六、写测试类
package com.pjx.dao;
import com.pjx.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.pjx.utils.MybatisUtils;
import java.util.List;
// 使用mybatis第六步/写test主方法调用
public class MyTest {
@Test
public void selectUser() {
SqlSession session = MybatisUtils.getSession();
//方法一:早期版本
// List<com.pjx.pojo.User> users = session.selectList("com.pjx.dao.UserDao.selectUser");
//方法二: 推荐使用
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.selectUser();
for (User user: users){
System.out.println(user);
}
session.close();
}
@Test
public void getUserbyId(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.getUserbyId(1);
System.out.println(user);
session.close();
}
@Test
public void addUser(){
SqlSession session = MybatisUtils.getSession();
UserMapper mapper = session.getMapper(UserMapper.class);
System.out.println(mapper.addUser(new User(6,"胡","26")));
session.commit();
session.close();
}
}