原来的pom.xml文件是这样的
然后加了一句注释
接着在注释下面添加下面的一段代码
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/ *.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
mybatis-config.xml是mybat的核心配置文件
1.选择main下面的resources文件夹
2.右键,new->File
3.输入文件名,创建完毕
也是在resource文件夹下面,new->File,输入db.properties,创建完毕
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username=root
password=123456
在mybatis-config.xml粘贴下面的代码,这里我先把注释掉了,后面会说
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
configuration>
我们用引入了刚刚写的db.properties文件,${driver}等这些就是取得db.properties文件里面设置的值
1.选中main下的java文件夹
2.右键,new->Package
3.输入包名com.zzh
然后在com.zzh包下,再建三个包,分别是dao,pojo,utils
包创建完成
右键utils包,new->Java Class,输入MybatisUtils
MybatisUtils.java的完整代码如下
package com.zzh.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;
/**
* @author zzh
* @create 2020
*/
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 (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
工具类创建完成
1.右键pojo包,new->Java Class
2.输入User,确定
3.User.java的完整代码如下
package com.zzh.pojo;
/**
* @author zzh
* @create 2020
*/
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 + '\'' +
'}';
}
}
1.右键dao包,new->Java Class
2.输入UserMapper,并选择Interface,确定
3.UserMapper接口的完整代码如下
package com.zzh.dao;
import com.zzh.pojo.User;
import java.util.List;
/**
* @author zzh
* @create 2020
*/
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据ID查询用户
User getUserById(int id);
//insert插入一个用户
int addUser(User user);
//update修改用户
int updateUser(User user);
//delete删除用户
int deleteUser(int id);
}
1.右键dao包,new->File
2.输入UserMapper.xml,确定
3.UserMapper.xml的完整代码如下
<mapper namespace="com.zzh.dao.UserMapper">
<select id="getUserList" resultType="com.zzh.pojo.User">
select * from user
select>
<select id="getUserById" parameterType="int" resultType="com.zzh.pojo.User">
select * from user where id = #{id}
select>
<insert id="addUser" parameterType="com.zzh.pojo.User">
insert into user (id, name, pwd) values (#{id} ,#{name} ,#{pwd})
insert>
<update id="updateUser" parameterType="com.zzh.pojo.User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
delete>
mapper>
4.到这里就可以回到mybatis-config.xml那里,把注释取消掉了
1.选中test文件夹下的绿色java文件夹
2.右键,new->Package
3.输入com.zzh.dao,确定
4.选中刚创建的dao包,new->Java Class,输入UserMapperTest,确定
5.UserMapperTest.java的完整代码如下
package com.zzh.dao;
import com.zzh.pojo.User;
import com.zzh.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
/**
* @author zzh
* @create 2020
*/
public class UserMapperTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
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();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.addUser(new User(4,"王五","123321"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"赵六","123321"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
1.创建数据库mybatis
2.在里面创建一个user表,表的设置如下
3.在里面输入一些数据
全部工作就完成了