本例在使用MyBatis Generator工具操作MySQL数据库示例基础上操作。
1.新建GetSqlSessionFactory.java类,完整代码如下:
package dbtools;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class GetSqlSessionFactory {
private static SqlSessionFactory sqlSessionFactory;
private GetSqlSessionFactory() {
}
synchronized public static SqlSessionFactory getSqlSessionFactory() {
try {
if(sqlSessionFactory==null) {
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}else {
}
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
return sqlSessionFactory;
}
}
这里使用单例设计模式来取得SqlSessionFactory对象。
2.创建GetSqlSession.java类,核心代码如下:
package dbtools;
import org.apache.ibatis.session.SqlSession;
public class GetSqlSession {
private static ThreadLocal<SqlSession> tl=new ThreadLocal<SqlSession>();
public static SqlSession getSqlSession() {
SqlSession sqlSession=tl.get();
if (sqlSession==null) {
sqlSession=GetSqlSessionFactory.getSqlSessionFactory().openSession();
tl.set(sqlSession);
}else {
}
System.out.println("获得的sqlSession对象的hashCode:"+sqlSession.hashCode());
return sqlSession;
}
public static void commit() {
if(tl.get()!=null) {
tl.get().commit();
tl.get().close();
tl.set(null);
System.out.println("提交了");
}
}
public static void rollback() {
if (tl.get()!=null) {
tl.get().rollback();
tl.get().close();
tl.set(null);
System.out.println("回滚了");
}
}
}
3.创建DBOperate.java类,核心代码如下:
package dbtools;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
public class DBOperate {
public int insert(String sqlId,Map valueMap) {
//注意这里将sqlSession创建在方法内部
SqlSession sqlSession=GetSqlSession.getSqlSession();
return sqlSession.insert(sqlId,valueMap);
}
public int delete(String sqlId,Map valueMap) {
SqlSession sqlSession=GetSqlSession.getSqlSession();
return sqlSession.delete(sqlId,valueMap);
}
public int update(String sqlId,Map valueMap) {
SqlSession sqlSession=GetSqlSession.getSqlSession();
return sqlSession.update(sqlId,valueMap);
}
public List<Map> select(String sqlId,Map valueMap) {
SqlSession sqlSession=GetSqlSession.getSqlSession();
return sqlSession.selectList(sqlId,valueMap);
}
}
所有的CRUD参数值都用Map对象进行封装。
4.userinfoMapping.xml映射文件及mybatis-config.xml配置文件,数据库设计等参看使用MyBatis Generator工具操作MySQL数据库示例,在userinfoMapping.xml映射文件<mapper>标签中添加如下内容:
<insert id="insertUserinfo" parameterType="map" useGeneratedKeys="true" keyProperty="id">
insert into userinfo(username,password,age) values(#{username},#{password},#{age})
</insert>
<select id="getUserinfoById" parameterType="map" resultType="map">
select * from userinfo where id=#{id}
</select>
<delete id="deleteUserinfoById" parameterType="map">
delete from userinfo where id=#{id}
</delete>
<select id="getAllUserinfo" resultType="map">
select * from userinfo
</select>
<update id="updateUserinfoById" parameterType="map">
update userinfo set username=#{username},password=#{password},age=#{age} where id=#{id}
</update>
6.修改Test.java文件进行测试
package test;
import java.util.HashMap;
import dbtools.DBOperate;
import dbtools.GetSqlSession;
public class Test {
public static void main(String[] args) {
try {
HashMap valueMap1=new HashMap();
valueMap1.put("username", "张三");
valueMap1.put("password","123456");
valueMap1.put("age", 100);
HashMap valueMap2=new HashMap();
valueMap2.put("username", "张三");
valueMap2.put("password","123456");
valueMap2.put("age", 100);
DBOperate dbo=new DBOperate();
dbo.insert("insertUserinfo", valueMap1);
dbo.insert("insertUserinfo", valueMap2);
} catch (Exception e) {
e.printStackTrace();
GetSqlSession.rollback();
} finally {
GetSqlSession.commit();
}
}
}
Output:
获得的sqlSession对象的hashCode:1684106402
获得的sqlSession对象的hashCode:1684106402
提交了
此时可以看到数据库中多了两条数据。