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>cn.sicko</groupId>
<artifactId>Mybatis-Study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis-01</module>
</modules>
<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.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
<!-- 在build中配置resources,来防止我们资源导出失败的问题-->
<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>
Mybatis-config.xml
<?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核心配置文件-->
<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="123456"/>
</dataSource>
</environment>
</environments>
<!-- 每一个Mapper.xml都需要在Mybatis核心配置文件中注册-->
<mappers>
<mapper resource="cn/sicko/dao/UserMapper.xml"></mapper>
</mappers>
</configuration>
工具类MybatisUtils
package cn.sicko.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;
// sqlSessionFactory --> sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL 语句。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
根据数据库数据写实体类User
package cn.sicko.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 + '\'' +
'}';
}
}
接口UserMapper
package cn.sicko.dao;
import cn.sicko.pojo.User;
import java.util.List;
public interface UserMapper {
// 查询全部用户
List<User> getUserList();
// 根据ID查询用户
User getUserById(int id);
// insert一个用户
int addUser(User user);
// 修改用户
int updateUser(User user);
// 删除一个用户
int deleteUser(int id);
}
与mapper对应的sql语句UserMapper.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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="cn.sicko.dao.UserMapper">
<!-- select查询全部用户 -->
<select id="getUserList" resultType="cn.sicko.pojo.User">
select * from mybatis.user
</select>
<!-- select根据ID查询 -->
<select id="getUserById" parameterType="int" resultType="cn.sicko.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<!-- 对象中的属性,可以直接取出来 -->
<insert id="addUser" parameterType="cn.sicko.pojo.User">
insert into mybatis.user (id,name,pwd)
values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="cn.sicko.pojo.User">
update mybatis.user
set name=#{name},pwd=#{pwd}
where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete
from mybatis.user
where id=#{id};
</delete>
</mapper>
测试类UserTest
package cn.sicko.dao;
import cn.sicko.pojo.User;
import cn.sicko.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserTest {
@Test
public void Test1(){
// 第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
// 方式一:getMapper执行SQL
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
// 方式二:
// List<User> userList = sqlSession.selectList("cn.sicko.dao.UserMapper.getUserList");
for (User user : userList) {
System.out.println(user);
}
}catch (Exception ex){
ex.printStackTrace();
}finally {
// 关闭SqlSession
sqlSession.close();
}
}
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
// 增删改需要提交事物
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.addUser(new User(4,"小心","156247"));
if(i>0){
System.out.println("插入数据成功");
}else{
System.out.println("插入数据失败");
}
// 提交事物
sqlSession.commit();
}catch (Exception ex){
ex.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.updateUser(new User(4, "呵呵", "111111"));
if(i>0)
System.out.println("更改成功");
else
System.out.println("失败");
sqlSession.commit();
}catch (Exception ex){
ex.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUser(4);
if(i>0)
System.out.println("成功");
else
System.out.println("失败");
sqlSession.commit();
}catch (Exception ex){
ex.printStackTrace();
}finally {
sqlSession.close();
}
}
}
注意事项:
- 增删改需要提交事物
sqlSession.commit();
- Sql语句参数
· id:对应的namespace中的方法名
· resultType:返回参数类型
· parameterType:传参数类型 - Maven项目可能会有资源找不到情况
<!-- 在build中配置resources,来防止我们资源导出失败的问题-->
<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>
4.绑定路径
2022/4/25
项目下载