mybatis - CRUD操作
sadness安全宇航员 lv.1
发布时间:2023-10-23 09:16:12 394相关标签:
一、CRUD操作
映射文件
<insert id="addUser">
insert into t_user values(null,'张三','123456')
insert>
<delete id="deleteUserById">
delete from t_user where id = 3
delete>
<update id="updateUser">
update t_user set username = '王五' where id = 1
update>
<select id="getAllUser" resultType="user">
select * from t_user
select>
<select id="getUser" resultType="user">
select * from t_user where id = 1
select>
mapper接口
public interface UserMapper {
int addUser();
int deleteUserById();
int updateUser();
List<User> getAllUser();
User getUser();
}
二、配置log4j日志
1.配置log4j 依赖
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
2.配置文件 ,文件名为log4j.xml
version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
layout>
appender>
<logger name="java.sql">
<level value="debug" />
logger>
<logger name="org.apache.ibatis">
<level value="info" />
logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
root>
log4j:configuration>
三、创建SqlSession工具类来获取SqlSession
public class SQLSessionUtils {
public static SqlSession getSqlSession() {
SqlSession sqlSession = null;
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
// openSession(true) 设置为自动提交事务
sqlSession = factory.openSession(true);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
}
文章来源: https://blog.51cto.com/u_15497049/5878330
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报