一、使用 IDEA 快速建立 Maven 專案
關於如何快速建立 Maven 專案,這個可以參考下面這篇文章:
二、快速配置 MyBatis 依賴 jar 包
開啟 pom.xml
檔案,快速配置 jar 包,配置程式碼如下:
<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>
複製程式碼
三、快速建立實體類
建立 pojo 類 User.java
,程式碼如下:
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 + "]";
}
}
複製程式碼
四、快速建立實體類對應的資料表
新建資料庫 mybatis_db,建立表 t_user,建表程式碼如下:
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;
複製程式碼
五、快速建立 Mapper 介面
建立介面 UserDao.java
,程式碼如下:
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);
}
複製程式碼
六、快速建立 Mapper 介面代理檔案
建立 xml 檔案 UserDaoMapper.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">
<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>
複製程式碼
七、快速新增 MyBatis 配置檔案
建立 xml 檔案 mybatis-comfig.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>
<!-- 配置 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>
複製程式碼
八、快速建立測試類
建立測試類 Test.java
,程式碼如下:
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();
}
}
複製程式碼
九、測試執行,結果如下圖:
本程式和資料庫相關的程式碼已經上傳到 GitHub 上,GitHub 地址:點選這裡直接獲取專案原始碼