1、实现类获取session
//根据id 修改阈值
public int altThers(threshold threshold) {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
try {
Reader reader = Resources.getResourceAsReader("/mybatis-config.xml");
SqlSessionFactory factory = builder.build(reader);
SqlSession session = factory.openSession(true);
session.getMapper(thresholdMapper.class).updateByPrimaryKeySelective(threshold);
session.commit();
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
2、mybatis配置文件
<?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>
<!--环境配置,连接的数据库,这里使用的是MySQL-->
<environments default="mysql">
<environment id="mysql">
<!--指定事务管理的类型,这里简单使用Java的JDBC的提交和回滚设置-->
<transactionManager type="JDBC"></transactionManager>
<!--dataSource 指连接源配置,POOLED是JDBC连接对象的数据源连接池的实现 serverTimezone=GMT-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/testapp?useUnicode=true&characterEncoding=utf8&
useSSL=false&serverTimezone=UTC" ></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名,
如果在某包下,则要写明路径,如:com/mybatistest/config/User.xml 直接将自动生成的sqlMapper复制过来即可-->
<mapper resource="sqlMapper/deptstateMapper.xml"></mapper>
<mapper resource="sqlMapper/thresholdMapper.xml"></mapper>
<mapper resource="sqlMapper/persionsMapper.xml"></mapper>
</mappers>
</configuration>