}
Mapper文件:
<?xml version="1.0" encoding="UTF-8" ?>
select * from Blog where id = #{id}
insert into Blog(id, title, author_id, state, featured, style)
values
(#{id}, #{title}, #{authorId}, #{state}, #{featured}, #{style});
定义接口:
package com.zy.mapper;
import com.huawei.entity.Blog;
public interface BlogMapper {
//接口中的方法名必须和mapper中sql语句的id相同
Blog selectBlog(Integer id);
//插入
void insert(Blog blog);
}
对应的类型:
定义DAO:
import com.huawei.entity.Blog;
import com.huawei.mapper.BlogMapper;
import com.huawei.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
public class BlogDao {
private SqlSession sqlSession = MyBatisUtil.getSqlSession();
private BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
public Blog queryBlogById(Integer id) {
return mapper.selectBlog(id);
}
public void insert(Blog blog) {
try {
mapper.insert(blog);
sqlSession.commit();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
工具类的编写:
package com.zy.util;
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.IOException;
import java.io.Reader;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
Reader reader = Resources.getResourceAsReader(“mybatis/mybatis-config.xml”);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
catch (IOException e) {
e.printStackTrace();
}
}
public MyBatisUtil() {
}
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
编写测试类:
package com.zy.mapper;
import com.huawei.dao.BlogDao;
import com.huawei.entity.Blog;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
public class BlogMapperTest {
private static final Logger logger = LogManager.getLogger(BlogMapperTest.class);
@Test
public void testSelectBlog() {
Blog blog = new BlogDao().queryBlogById(1);
System.out.println(blog);
logger.info(“blog is:{}”, blog);
}
@Test
public void testInsertBlog() {
Blog build = Blog.builder()
.authorId(1)
.state(“ACTIVE”)
.style(“333”)
.featured(false)
.title(“三国演义”)
.build();
new BlogDao().insert(build);
}
}
运行结果:
问题:
1.为什么代码中的sqlSession需要关闭,但是获取配置文件的Reader不需要关闭呢???
看源码:
查看build()方法:
public SqlSessionFactory build(Reader reader) {
return this.build((Reader)reader, (String)null, (Properties)null);
}
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
SqlSessionFactory var5;
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
var5 = this.build(parser.parse());
} catch (Exception var14) {
throw ExceptionFactory.wrapException(“Error building SqlSession.”, var14);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException var13) {
}
}
return var5;
}
我们发现,reader已经被关闭,因此不需要我们关闭
2.为什么插入数据时,需要手动提交呢???难道不能自动提交吗???
查看SqlSession的创建过程:
sqlSession = sqlSessionFactory.openSession();
进入DefaultSqlSessionFactory:
public SqlSession openSession() {
return this.openSessionFromDataSource(this.configuration.getDefaultExecutorType(), (TransactionIsolationLevel)null, false);
}
我们发现最后一个参数时false,这个参数时干嘛呢???继续往下看
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
DefaultSqlSession var8;
try {
Environment environment = this.configuration.getEnvironment();
TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
Executor executor = this.configuration.newExecutor(tx, execType);
var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
} catch (Exception var12) {
this.closeTransaction(tx);
throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
} finally {
ErrorContext.instance().reset();
}
return var8;
}
这个参数,其实就是autoCommit的参数,默认是false,表示不能自动提交,必须手动提交。
那么有没有支持传值的函数呢???
public SqlSession openSession(boolean autoCommit) {
return this.s 需要zi料+ 绿色徽【vip1024b】
qlSessionFactory.openSession(autoCommit);
}
显然是存在这样函数的,我们可以自己传参,来设置是否支持自动提交。
我的工具类可以改造为一个抽象类:
总结
一般像这样的大企业都有好几轮面试,所以自己一定要花点时间去收集整理一下公司的背景,公司的企业文化,俗话说「知己知彼百战不殆」,不要盲目的去面试,还有很多人关心怎么去跟HR谈薪资。
这边给大家一个建议,如果你的理想薪资是30K,你完全可以跟HR谈33~35K,而不是一下子就把自己的底牌暴露了出来,不过肯定不能说的这么直接,比如原来你的公司是25K,你可以跟HR讲原来的薪资是多少,你们这边能给到我的是多少?你说我这边希望可以有一个20%涨薪。
最后再说几句关于招聘平台的,总之,简历投递给公司之前,请确认下这家公司到底咋样,先去百度了解下,别被坑了,每个平台都有一些居心不良的广告党等着你上钩,千万别上当!!!
Java架构学习资料,学习技术内容包含有:Spring,Dubbo,MyBatis, RPC, 源码分析,高并发、高性能、分布式,性能优化,微服务 高级架构开发等等。
还有Java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板可以领取+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书。
以自己一定要花点时间去收集整理一下公司的背景,公司的企业文化,俗话说「知己知彼百战不殆」,不要盲目的去面试,还有很多人关心怎么去跟HR谈薪资。
这边给大家一个建议,如果你的理想薪资是30K,你完全可以跟HR谈33~35K,而不是一下子就把自己的底牌暴露了出来,不过肯定不能说的这么直接,比如原来你的公司是25K,你可以跟HR讲原来的薪资是多少,你们这边能给到我的是多少?你说我这边希望可以有一个20%涨薪。
最后再说几句关于招聘平台的,总之,简历投递给公司之前,请确认下这家公司到底咋样,先去百度了解下,别被坑了,每个平台都有一些居心不良的广告党等着你上钩,千万别上当!!!
Java架构学习资料,学习技术内容包含有:Spring,Dubbo,MyBatis, RPC, 源码分析,高并发、高性能、分布式,性能优化,微服务 高级架构开发等等。
还有Java核心知识点+全套架构师学习资料和视频+一线大厂面试宝典+面试简历模板可以领取+阿里美团网易腾讯小米爱奇艺快手哔哩哔哩面试题+Spring源码合集+Java架构实战电子书。
[外链图片转存中…(img-D8lvFzgl-1710361965434)]