使用MyBatis 框架进行数据层(持久层)开发
程序员文章站
2022-06-02 15:15:36
...
具体做法参考来自:轻量级框架项目教程part1
项目工程截图:
UserInfo.class:
package com.mybatis.po; import java.util.Date; public class UserInfo { private int id; private String userName; private String password; private Date regDate; public UserInfo() { super(); } public UserInfo(String userName, String password, Date regDate) { super(); this.userName = userName; this.password = password; this.regDate = regDate; } public UserInfo(String userName, String password) { super(); this.userName = userName; this.password = password; } 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 getRegDate() { return regDate; } public void setRegDate(Date regDate) { this.regDate = regDate; } }
UserMapper.xml:
主要实现sql语句
<?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.po.userInfoMapper"> <!-- 数据表user_info的CRUD的操作 mysql 的xml 文件 --> <!-- 映射插入语句 --> <insert id="addUserInfo" parameterType="UserInfo"> insert into user_info(userName,password,regDate) values(#{userName},#{password},#{regDate}); </insert> <!-- delete语句 --> <delete id="deleteUserInfo" parameterType="int"> delete from user_info where id=#{id} </delete> <!-- 更新update 语句 --> <update id="updateUserInfo" parameterType="UserInfo"> update user_info set userName=#{userName},password=#{password},regDate=#{regDate} where id=#{id} </update> <!-- select 根据id查 parameterType 参数类型 --> <select id="getUserInfoById" parameterType="int" resultType="UserInfo"> select * from user_info where id=#{id} </select> <!-- select 查询所有记录 --> <select id="getAllUserInfo" resultType="UserInfo"> select * from user_info </select> </mapper>
dp.properties:
视频中的是这个:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/digital jdbc.username=root jdbc.password=root1234
我的是这个:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/digital?useSSL=false jdbc.username=root jdbc.password=root1234
不加?useSSL=false 会有:
Wed Apr 29 11:11:17 CST 2020 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
以及多了一个log4j.properties:
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
不加报错:
mybatis的log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
解决参考来自:博客
mybaties-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> <!-- 加载属性文件 db.properties--> <properties resource="db.properties"></properties> <typeAliases> <package name="com.mybatis.po"/> <!-- 注册一个别名 直接使用类名 无需前面加包名 --> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <!-- 事务的管理器 --> <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/mybatis/po/userInfoMapper.xml"/> </mappers> </configuration>
MybatiesTest :
我按照视频写的@before 不能运行,我就索性全写在tes里面了。
package com.mybatis.test;
import static org.junit.Assert.*;
import java.io.InputStream;
import java.util.Date;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import com.mybatis.po.UserInfo;
//测试类
class MybatisTest {
private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
//读取配置文件信息
@Test
void testAddUserInfo() {
String resource="mybatis-config.xml";
System.out.println("来到了@Test");
System.out.println("resource");
InputStream inputStream;
try{
//通过mybatis的配置文件得到一个输入流
inputStream=Resources.getResourceAsStream(resource);
//创建会话工厂 一个构件器 的build 方法
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//得到session
sqlSession=sqlSessionFactory.openSession();
}
catch(Exception e) {
e.printStackTrace();
}
System.out.println("输出sqlSession=="+sqlSession);
UserInfo ui=new UserInfo("mybatis","123456",new Date());
UserInfo ui1=new UserInfo("mybatis","123456");
System.out.println("输出resource=="+resource);
sqlSession.insert("addUserInfo",ui);
System.out.println("插入语句执行完成");
sqlSession.commit();
//关闭session
sqlSession.close();
}
}
推荐阅读
-
持久层框架JPA与Mybatis该如何选型
-
Mybatis持久层框架技术
-
.netCore+Vue 搭建的简捷开发框架 (2)--仓储层实现和EFCore 的使用
-
bbossgroups持久层框架数据源配置文件实例
-
Nano Framework之使用Mybatis进行持久层开发
-
Nano Framework之添加H2数据库并使用JDBC进行持久层开发
-
SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作
-
Mybatis框架三:DAO层开发、Mapper动态代理开发
-
Hibernate 4 beta3 发布,Java数据持久层框架
-
使用mybatis开发原始Dao层