ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 创建数据库信息 ```mysql CREATE DATABASE `mybatis` CHARACTER SET 'utf8' COLLATE 'utf8_bin'; USE `mybatis`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255), `age` int(11), `address` varchar(255), PRIMARY KEY (`id`) USING BTREE ); ``` ### 创建项目 > 名为:mybatis ```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>com.gosuncn</groupId> <artifactId>mybatis</artifactId> <version>1.0</version> <dependencies> <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.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*Mapper.xml</include> </includes> </resource> </resources> </build> </project> ``` > 其中,`<resources>...</resources>`是为了使`UserMapper`接口和`UserMapper.xml`映射文件可以在同一个目录下,看着更有条理。 ### 创建全局配置`SqlMapConfig.xml`文件 ```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> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/gosuncn/dao/UserMapper.xml"/> </mappers> </configuration> ``` ### 创建实体类`User` ```java package com.gosuncn.entity; import java.io.Serializable; public class User implements Serializable { private Integer id; private String name; private Integer age; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } } ``` ### 创建持久层接口 > 名为:UserMapper ```java package com.gosuncn.dao; import com.gosuncn.entity.User; public interface UserMapper { void saveUser(User user); } ``` ### 创建映射文件 > 在与`UserMapper`接口相同目录下,创建名为`UserMapper.xml`的映射文件 ```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.gosuncn.dao.UserMapper"> <insert id="saveUser" parameterType="com.gosuncn.entity.User"> insert into user (name, age, address) values (#{name}, #{age}, #{address}) </insert> </mapper> ``` ### 创建测试类 ```java package com.gosuncn; import com.gosuncn.dao.UserMapper; import com.gosuncn.entity.User; 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.InputStream; public class MainTest { public static void main(String[] args) throws Exception { InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sessionFactory.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setName("ABC"); user.setAge(26); user.setAddress("BeiJin"); userMapper.saveUser(user); sqlSession.commit(); // 如若不提交,程序则会回滚。 sqlSession.close(); inputStream.close(); } } ``` 其中,`sqlSession.commit();`该代码若没写,事务将会回滚,数据库中则看不到新增数据。