目录
mysql数据库
/* Navicat MySQL Data Transfer Source Server : lijunxiang Source Server Version : 80019 Source Host : localhost:3306 Source Database : test Target Server Type : MYSQL Target Server Version : 80019 File Encoding : 65001 Date: 2021-08-21 21:39:50 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for user01 -- ---------------------------- DROP TABLE IF EXISTS `user01`; CREATE TABLE `user01` ( `id` int NOT NULL AUTO_INCREMENT, `username` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, `birthday` bigint DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user01 -- ---------------------------- INSERT INTO `user01` VALUES ('1', 'bdaj', 'bdha', '1629548933591'); INSERT INTO `user01` VALUES ('2', 'bdaj', 'bdha', '1629549730181'); INSERT INTO `user01` VALUES ('3', 'bdaj', 'bdha', '1629549734294'); INSERT INTO `user01` VALUES ('4', 'bdaj', 'bdha', '1629549739579');
User实体
package com.ljx.domain; import java.util.Date; public class User { private int id; private String username; private String password; private Date birthday; 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 Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", birthday=" + birthday + '}'; } }
DateTypeHandler
package com.ljx.handler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Date; public class DateTypeHandler extends BaseTypeHandler<Date> { @Override // 将java类型转换为数据库需要的类型 public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { long time = date.getTime(); preparedStatement.setLong(i,time); } @Override //将数据库类型转换为java类型 // String参数 要转换的字段名称 // ResultSet 查询出结果集 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { // 获得结果集中需要的数据(long)转换成Date类型 返回 long aLong = resultSet.getLong(s); Date date = new Date(aLong); return date; } @Override //将数据库类型转换为java类型 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { long aLong = resultSet.getLong(i); Date date = new Date(aLong); return date; } @Override //将数据库类型转换为java类型 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { long aLong = callableStatement.getLong(i); Date date = new Date(aLong); return date; } }
UserMapper接口
package com.ljx.mapper; import com.ljx.domain.User; import java.util.List; public interface UserMapper { void save(User user); List<User> findAll(); }
resource:配置文件
com/ljx/mapper/User.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybbatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.ljx.mapper.UserMapper"> <insert id="save" parameterType="user"> insert into user01 values (#{id},#{username},#{password},#{birthday}) </insert> <select id="findAll" resultType="user"> select * from user01 </select> </mapper>
jdbc.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=asd.123
sqlMapConfig.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> <!-- 通过properties标签加载外部properties文件--> <properties resource="jdbc.properties"></properties> <!-- 定义别名--> <typeAliases> <typeAlias type="com.ljx.domain.User" alias="user"></typeAlias> </typeAliases> <!-- 注册类型处理器--> <typeHandlers> <typeHandler handler="com.ljx.handler.DateTypeHandler"/> </typeHandlers> <!-- 配置分页助手插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins> <!-- 数据源对象--> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 加载映射文件--> <mappers> <mapper resource="com\ljx\mapper\User.xml"></mapper> </mappers> </configuration>
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>org.example</groupId> <artifactId>mybatis_config</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>mybatis_config Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency> </dependencies> </project>