由于第一次写服务端,犯了很多错误,自己写的JDBC连接在运行时一直断开导致数据没办法实时保存,查询后决定使用mybatis.因为以前是Android开发,所以选用了gradle构建项目。
步骤一、首先在build.gradle中添加要用到的依赖
implementation 'mysql:mysql-connector-java:8.0.33'
implementation 'org.mybatis:mybatis:3.5.4'
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.1'
步骤二、配置mybatis
在recourses目录中添加mybatis-config.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="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="closeConnection" value="false"/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/face"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/myMapper.xml"/>
</mappers>
</configuration>
其中closeConnection设置为false,数据库连接不断开。
<mapper resource="mybatis/myMapper.xml"/>
myMapper.xml文件用于定于数据库操作的SQL语句,下面是myMapper.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="org.hkwy.mapper.RecordMapper">
<insert id="addKaoQin" parameterType="org.hkwy.bean.KaoQin">
INSERT INTO KaoQin (name, job_num,time,pass,kqj_num) VALUES (#{name}, #{job_num},#{time},#{pass},#{kqj_num})
</insert>
<select id="queryKaoQinList" resultType="org.hkwy.bean.KaoQin">
SELECT *FROM kaoqin
</select>
<update id="updateKaoQin" parameterType="org.hkwy.bean.KaoQin">
UPDATE kaoqin set job_num = #{job_num} WHERE name = #{name}
</update>
</mapper>
步骤三、创建数据模型
public class KaoQin {
private String job_num;//员工编号
private String name;//员工姓名
private String time;//打开时间
public String getJob_num() {
return job_num;
}
public void setJob_num(String job_num) {
this.job_num = job_num;
}
public String getKqj_num() {
return kqj_num;
}
public void setKqj_num(String kqj_num) {
this.kqj_num = kqj_num;
}
private String pass;// 1-通过 0-不通过
private String kqj_num;//考勤机编号
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
步骤四、定义数据库操作接口(与myMapper.xml中的sql相对应)
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.hkwy.bean.KaoQin;
import java.util.List;
public interface RecordMapper extends BaseMapper<KaoQin>{
/**
* 插入一条打卡记录
* @param recogniseResult
* @return
*/
Integer addKaoQin(KaoQin recogniseResult);
List<KaoQin> queryKaoQinList();
void updateKaoQin(KaoQin recogniseResult);
}
步骤五、执行
try {
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
RecordMapper recordMapper = sqlSession.getMapper(RecordMapper.class);
KaoQin record=new KaoQin();
record.setJob_num("293");
record.setName("lilii");
record.setPass("1");
record.setTime("2024-02-24 10:10:59");
record.setKqj_num("8169023060000990");
int value=recordMapper.addKaoQin(record);
if (sqlSession != null) {
sqlSession.commit();
sqlSession.close();
}
}catch (IOException e){
e.printStackTrace();
}
}