1. Use IDEA to quickly create a Maven project
For how to quickly create a Maven project, you can refer to the following article:
Maven project management tool basic introduction series (1)
2. Quickly configure MyBatis dependency jar package
Open the pom.xml
file and quickly configure the jar package. The configuration code is as follows:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mybatis</groupId>
<artifactId>MyBatisDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>MyBatisDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- MyBatis 依赖包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!-- 数据库驱动依赖包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- junit 测试依赖包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>MyBatisDemo</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
3. Quickly create entity classes
Create a pojo class User.java
with the following code:
package com.mybatis.pojo;
public class User{
private int id;
private String username;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", age=" + age + "]";
}
}
Fourth, quickly create the data table corresponding to the entity class
Create a new database mybatis_db, create a table t_user, and the table creation code is as follows:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
5. Quickly create Mapper interface
Create an interface UserDao.java
, the code is as follows:
package com.mybatis.dao;
import com.mybatis.pojo.User;
public interface UserDAO {
//新增用户
public int addUser(User user);
//根据id删除用户
public int deleteUser(int id);
//修改用户
public int updateUser(User user);
//根据id查询用户
public User selectUserById(int id);
}
6. Quickly create a Mapper interface proxy file
Create an xml file UserDaoMapper.xml
with the following code:
<?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.mybatis.dao.UserDao">
<insert id="addUser" parameterType="com.mybatis.pojo.User">
insert into t_user (username,password,age) values (#{username},#{password},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from t_user where id=#{id}
</delete>
<update id="updateUser" >
update t_user set username=#{username},password=#{password},age=#{age} where id=#{id}
</update>
<select id="selectUserById" parameterType="int" resultType="com.mybatis.pojo.User">
select * from t_user where id=#{id}
</select>
</mapper>
Seven, quickly add MyBatis configuration file
Create an xml file mybatis-comfig.xml
with the following configuration:
<?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>
<!-- 配置 MyBatis 运行环境 -->
<environments default="development">
<environment id="development">
<!-- 配置 JDBC 事务管理 -->
<transactionManager type="JDBC" />
<!-- POOLED 配置 JDBC 数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<!-- 注册 UserDAO.xml -->
<mappers>
<mapper resource="com/mybatis/dao/UserDaoMapper.xml"/>
</mappers>
</configuration>
8. Quickly create test classes
Create a test class Test.java
with the following code:
package com.mybatis.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis.dao.UserDao;
import com.mybatis.pojo.User;
public class Test {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
UserDao userDAO = sqlSession.getMapper(UserDao.class);
//新增用户
User user = new User();
user.setUsername("孔乙己");
user.setPassword("123456");
user.setAge(21);
System.out.println(userDAO.addUser(user));
sqlSession.commit();
// //删除用户
// System.out.println(userDAO.deleteUser(1));
// sqlSession.commit();
//
// //查询用户
// User user1 = userDAO.selectUserById(1);
// System.out.println(user1);
//
// //修改用户
// User user2 = userDAO.selectUserById(1);
// user2.setUsername("指南者");
// System.out.println(userDAO.updateUser(user2));
// sqlSession.commit();
}
}
Nine, the test run, the results are as follows:
The code related to this program and database has been uploaded to GitHub, GitHub address: Click here to get the project source code directly