SqlSession sqlSession = null;
try {
sqlSession = DBUtils.openSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//查询同一条数据时会缓存
User user = mapper.getUser(1l);
User user2 = mapper.getUser(1l);
System.out.println(user.toString());
System.out.println(user2.toString());
sqlSession.commit();
} catch (Exception e) {
System.err.println(e.getMessage());
sqlSession.rollback();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.sang.db.UserMapper">
<cache/>
<select id="getUser" resultType="org.sang.bean.User" parameterType="Long">
select * from user where id = #{id}
</select>
<insert id="insertUser" parameterType="org.sang.bean.User">
INSERT INTO user(username,password,address) VALUES (#{username},#{password},#{address})
</insert>
<delete id="deleteUser" parameterType="Long">
DELETE FROM user where id=#{id}
</delete>
<select id="getAll" resultType="u">
SELECT * from user
</select>
</mapper>
public class User implements Serializable{
private Long id;
private String username;
private String password;
private String address;
...
}
SqlSession sqlSession = null;
SqlSession sqlSession2 = null;
try {
sqlSession = DBUtils.openSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUser(1l);
System.out.println(user.toString());
sqlSession.commit();
sqlSession2 = DBUtils.openSqlSession();
UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
User user2 = mapper2.getUser(1l);
System.out.println(user2.toString());
sqlSession2.commit();
} catch (Exception e) {
System.err.println(e.getMessage());
sqlSession.rollback();
sqlSession2.rollback();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
if (sqlSession2 != null) {
sqlSession2.close();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有