发表于: 2018-02-06 21:21:52
1 764
今天做了什么:
写完了service层,test都跑通了。
package com.ev.service;
import com.ev.dao.StudentsMapper;
import com.ev.pojo.Students;
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.InputStream;
public class StudentsService {
//声明私有对象sqlSessionFactory,避免线程问题
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "SqlMapConfig.xml"; //mybatis配置文件
//得到配置文件的流
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
//传入mybatis的配置文件的流,创建会话工厂SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
public Long insertStudents(Students students) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
//创建StudentsMapper对象,mybatis自动生成mapper代理对象
StudentsMapper studentsMapper = sqlSession.getMapper(StudentsMapper.class);
studentsMapper.insertStudents(students);
return students.getId();
}
public void deleteStudents(Long id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentsMapper studentsMapper = sqlSession.getMapper(StudentsMapper.class);
studentsMapper.deleteStudents(id);
}
public String findStudentsById(Long id) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentsMapper studentsMapper = sqlSession.getMapper(StudentsMapper.class);
return studentsMapper.findStudentsById(id).toString();
}
public String findStudentsByName(String name) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentsMapper studentsMapper = sqlSession.getMapper(StudentsMapper.class);
return studentsMapper.findStudentsByName(name).toString();
}
public void updateStudents(Students students) throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentsMapper studentsMapper = sqlSession.getMapper(StudentsMapper.class);
studentsMapper.updateStudents(students);
}
}
之前事务工厂初始化的代码是:
private SqlSessionFactory sqlSessionFctory;
public void setUp() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);}
测试一直报错,空指针。
之后把sqlSessionFactory声明加了static,把下面的getSession()换成static块,测试通过。
了解了下mybatis config文件 的各种标签
properties(属性)
将数据库连接参数单独配置在db.properties中,放在类路径下。这样只需要在SqlMapConfig.xml中加载db.properties的属性值。这样在SqlMapConfig.xml中就不需要对数据库连接参数硬编码。
将数据库连接参数只配置在db.properties中,原因:方便对参数进行统一管理,其它xml可以引用该db.properties。
如:
<properties resource="db.properties"/>
<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>
settings(全局配置参数)
mybatis全局配置参数,全局参数将会影响mybatis的运行行为。比如:开启二级缓存、开启延迟加载。(没接触过
typeAliases(类型别名)
typeAliases可以用来自定义别名。在mapper.xml中,定义很多的statement,而statement需要parameterType指定输入参数的类型、需要resultType指定输出结果的映射类型。如果在指定类型时输入类型全路径,不方便进行开发,可以针对parameterType或resultType指定的类型定义一些别名,在mapper.xml中通过别名定义,方便开发。
如:
<typeAliases>
<typeAlias alias="Students" type="com.ev.pojo.Students" />
</typeAliases>
typeHandlers(类型处理器)
mybatis中通过typeHandlers完成jdbc类型和java类型的转换。
objectFactory(对象工厂)
plugins(插件)
environments(环境集合属性对象)
MyBatis 可以配置多种环境。这会帮助你将 SQL 映射应用于多种数据库之中。但是要记得一个很重要的问题:你可以配置多种环境,但每个数据库对应一个 SqlSessionFactory。
--environment(环境子属性对象)
----transactionManager(事务管理)
----dataSource(数据源)
mappers(映射器)
Mapper配置的几种方法:
第一种(常用)
<mapper resource=" " />
resource指向的是相对于类路径下的目录
如:<mapper resource="sqlmap/User.xml" />
第二种
<mapper url=" " />
使用完全限定路径
如:<mapper url="file:///D:\workspace\mybatis1\config\sqlmap\User.xml" />
第三种
<mapper class=" " />
使用mapper接口类路径
如:<mapper class="cn.kang.mapper.UserMapper"/>
注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。
第四种(推荐)
<package name=""/>
注册指定包下的所有mapper接口
如:<package name="cn.kang.mapper"/>
注意:此种方法要求mapper接口名称和mapper映射文件名称相同,且放在同一个目录中。(很方便
配置了spring。
明天打算做什么:
mybatis还是有很多坑,明天再看看resultMap具体怎么用。
尝试今天看的标签。
spring配置完,尝试框架
问题:
空指针报错和service层的事务工厂规范。(基本解决
问师兄的问题都比较蠢,年前还是补一补基础。。。
收获:
mybatis的业务分层,和各层实现。
配置spring。
服务器没买,先缓一缓,之前还有很多地方不明所以。
评论