mybatis第4天
2016-03-27 19:47
357 查看
注册SqlSessionTemplate
<beanid="session"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-argref="sessionFactory"></constructor-arg>
</bean>
mybatis.cfg.xml只需配置typeAliases
@Transactional(readOnly=true)配置只读事务
public class ManServiceImpl implements IManService {
@Resource(name="session")
private SqlSessionTemplate sessionTemplate;
@Transactional(readOnly=false,rollbackFor=Exception.class) 配置回滚事务
public void add(ManBean bean) throws Exception{
this.sessionTemplate.insert("addMan",bean);
}
}
publicclass BaseDao{
private SqlSessionTemplate session;
/**
* 分页查询
* @param listSql 当前页数据的SQL
* @param countSql 总条数SQL
* @param queryMap 动态条件
* @param pageNO 页码
* @param pageCount 每页显示条数
* @return
* @throws Exception
*/
public CutPageBean cutPage(StringlistSql,String countSql,Map queryMap,int pageNO,int pageCount)throws Exception{
}
CutPageBeanbean = new CutPageBean();
//起始条数
queryMap.put("start",(pageNO-1)*pageCount);
//每页显示条数
queryMap.put("pageCount",pageCount);
//查询当前页数据
List list= this.getSession().selectList(listSql,queryMap);
bean.setList(list);
//查询总条数
int count=this.getClient()
.selectOne(countSql,queryMap);
bean.setCount(count);
mybatis支持动态SQL拼接,通过include标签可以链接指定的sql标签
<selectid="cutFindAllUser" resultType="user">
select * from t_user where 1=1
<includerefid="dynaUserSql"/>
limit #start#,#pageCount#;
</select>
<select id="countUser"resultType="int">
select count(*) from t_user where 1=1
<include refid="dynaUserSql"/> ;
</select>
mybatis支持动态SQL拼接,通过include标签可以链接指定的sql标签
<selectid="cutFindAllUser" resultType="user">
select * from t_user where 1=1
<includerefid="dynaUserSql"/>
limit #start#,#pageCount#;
</select>
<select id="countUser"resultType="int">
select count(*) from t_user where 1=1
<include refid="dynaUserSql"/> ;
</select>
使用mybatis注解开发,可以省去类配置文件,简洁方便。但是比较复杂的SQL和动态SQL还是建议书写类配置文件。
<!-- 定义接口,在接口方法中直接书写SQL -->
public interface IUserDao {
@Insert("insert intot_user(userName,classId) values(#{userName},#{classId})")
public void add(UserBean bean) throws Exception;
@Delete("deletefrom t_user where id=#{id}")
public void del(int id) throws Exception;
@Update("update t_userset userName=#{userName},classId=#{classId} where id=#{userId}")
publicvoid update(UserBean bean) throws Exception;
@Select("select * from t_class")
public List<ClassBean> findAll() throwsException;
在mybatis基本配置文件mybatis.cfg.xml中加入对接口的引用
<mappers>
<!--配置ORM映射接口,使用注解配置-->
<mapperclass="com.lovo.dao.IUserDao"/>
</mappers>
<beanid="session"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-argref="sessionFactory"></constructor-arg>
</bean>
mybatis.cfg.xml只需配置typeAliases
@Transactional(readOnly=true)配置只读事务
public class ManServiceImpl implements IManService {
@Resource(name="session")
private SqlSessionTemplate sessionTemplate;
@Transactional(readOnly=false,rollbackFor=Exception.class) 配置回滚事务
public void add(ManBean bean) throws Exception{
this.sessionTemplate.insert("addMan",bean);
}
}
publicclass BaseDao{
private SqlSessionTemplate session;
/**
* 分页查询
* @param listSql 当前页数据的SQL
* @param countSql 总条数SQL
* @param queryMap 动态条件
* @param pageNO 页码
* @param pageCount 每页显示条数
* @return
* @throws Exception
*/
public CutPageBean cutPage(StringlistSql,String countSql,Map queryMap,int pageNO,int pageCount)throws Exception{
}
CutPageBeanbean = new CutPageBean();
//起始条数
queryMap.put("start",(pageNO-1)*pageCount);
//每页显示条数
queryMap.put("pageCount",pageCount);
//查询当前页数据
List list= this.getSession().selectList(listSql,queryMap);
bean.setList(list);
//查询总条数
int count=this.getClient()
.selectOne(countSql,queryMap);
bean.setCount(count);
mybatis支持动态SQL拼接,通过include标签可以链接指定的sql标签
<selectid="cutFindAllUser" resultType="user">
select * from t_user where 1=1
<includerefid="dynaUserSql"/>
limit #start#,#pageCount#;
</select>
<select id="countUser"resultType="int">
select count(*) from t_user where 1=1
<include refid="dynaUserSql"/> ;
</select>
mybatis支持动态SQL拼接,通过include标签可以链接指定的sql标签
<selectid="cutFindAllUser" resultType="user">
select * from t_user where 1=1
<includerefid="dynaUserSql"/>
limit #start#,#pageCount#;
</select>
<select id="countUser"resultType="int">
select count(*) from t_user where 1=1
<include refid="dynaUserSql"/> ;
</select>
使用mybatis注解开发,可以省去类配置文件,简洁方便。但是比较复杂的SQL和动态SQL还是建议书写类配置文件。
<!-- 定义接口,在接口方法中直接书写SQL -->
public interface IUserDao {
@Insert("insert intot_user(userName,classId) values(#{userName},#{classId})")
public void add(UserBean bean) throws Exception;
@Delete("deletefrom t_user where id=#{id}")
public void del(int id) throws Exception;
@Update("update t_userset userName=#{userName},classId=#{classId} where id=#{userId}")
publicvoid update(UserBean bean) throws Exception;
@Select("select * from t_class")
public List<ClassBean> findAll() throwsException;
在mybatis基本配置文件mybatis.cfg.xml中加入对接口的引用
<mappers>
<!--配置ORM映射接口,使用注解配置-->
<mapperclass="com.lovo.dao.IUserDao"/>
</mappers>
相关文章推荐
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- MyBatis学习笔记(二)之关联关系
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Java简单实现SpringMVC+MyBatis分页插件
- MyBatis批量添加、修改和删除
- MyBatis与Hibernate的比较
- 浅析mybatis和spring整合的实现过程
- mybatis的动态sql详解(精)
- MyBatis实践之动态SQL及关联查询
- Mybatis实现增删改查及分页查询的方法
- MyBatis入门学习教程(一)-MyBatis快速入门
- Spring与Mybatis的整合方法有哪些
- 详解MyBatis直接执行SQL查询及数据批量插入
- 解决springmvc+mybatis+mysql中文乱码问题
- Spring3.1.1+MyBatis3.1.1的增、删、查、改以及分页和事务管理
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- MyBatis实践之DAO与Mapper