MyBatis使用注解进行开发
来源:互联网 发布:中老年春秋连衣裙淘宝 编辑:程序博客网 时间:2024/05/19 08:26
关于MyBatis进行开发,我自己目前接触到的有俩种方法,一种是用逆向工程(或自己写)生成Mapper文件,这种类型的有时间我会做个总结,还有一种就是使用注解的形式进行开发,个人比较喜欢使用这种形式进行一些简单的开发,看起来更加简单明了,而且不用去生成对应的Mapper的.xml文件
项目用的是Spring+MyBatis进行开发,具体的配置就不详细贴了
applicationContext:
<!-- 自动扫描该包,SpringMVC会将包下用了@controller注解的类注册为Spring的controller --> <context:component-scan base-package="csh.hrm.controller"/> <!-- 使用默认的Servlet来响应静态文件 --> <mvc:default-servlet-handler/> <!-- 定义Spring MVC的拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 拦截所有请求 --> <mvc:mapping path="/*"/> <!-- 自定义判断用户权限的拦截类 --> <bean class="csh.hrm.interceptor.AuthorizedInterceptor"/> </mvc:interceptor> </mvc:interceptors> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 内部资源视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 上传文件大小上限,单位为字节(10MB) --> <property name="maxUploadSize"> <value>10485760</value> </property> <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 --> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean></beans>
然后Dao层写接口,通过注解形式去写具体的SQL语句
public interface DeptDao { @SelectProvider(type=DeptDynaSqlProvider.class,method="selectWhitParam") List<Dept> selectWhitParam(Map<String, Object> params); @SelectProvider(type=DeptDynaSqlProvider.class,method="Count") Integer Count(Map<String, Object> params); @Select("select * from "+DEPTTABLE+" where id=#{id}") Dept findDeptById(Integer id); @UpdateProvider(type=DeptDynaSqlProvider.class,method="updateDept") void updateDept(Dept dept); @InsertProvider(type=DeptDynaSqlProvider.class,method="insertDept") void insertDept(Dept dept); @Delete("delete from "+DEPTTABLE+" where instr(concat(',',#{ids},','),concat(',',id,',')) > 0 ") void deleteDeptById(String ids); //所有部门的ID,NAME @Select("select ID,NAME from "+DEPTTABLE+ " where 1=1") List<Dept> findAllDept();}
动态SQL类DeptDynaSqlProvider:
public class DeptDynaSqlProvider { public String Count(final Map<String, Object> params){ return new SQL(){ { SELECT("count(*)"); FROM(DEPTTABLE); if(params.get("dept") != null){ Dept dept=(Dept) params.get("dept"); if(dept.getName() != null && !dept.getName().equals("")){ WHERE(" name LIKE CONCAT ('%',#{dept.name},'%') "); } } } }.toString(); } public String selectWhitParam(final Map<String, Object>params){ String sql= new SQL(){ { SELECT("*"); FROM(DEPTTABLE); if (params.get("dept")!=null) { Dept dept=(Dept) params.get("dept"); if (dept.getName()!=null&&!dept.getName().equals("")) { WHERE(" name LIKE CONCAT ('%',#{dept.name},'%') "); } } } }.toString(); if(params.get("pageModel")!=null){ sql += " limit #{pageModel.firstLimitParam} , #{pageModel.pageSize} "; } return sql; } public String updateDept(final Dept dept){ return new SQL(){ { UPDATE(DEPTTABLE); if (dept.getName()!=null) { SET(" name=#{name}"); } if (dept.getRemark()!=null) { SET(" remark=#{remark}"); } WHERE(" id=#{id}"); } }.toString(); } public String insertDept(final Dept dept){ return new SQL(){ { INSERT_INTO(DEPTTABLE); if (dept.getName()!=null) { VALUES("name", "#{name}"); } if (dept.getRemark()!=null) { VALUES("remark","#{remark}"); } } }.toString(); }}
如果有一对一,一对多,多对一等关系的,可以使用@Results注解去指定数据库中的具体字段映射在实体类中的具体属性,例如:
@SelectProvider(type=EmployeeDynaSqlProvider.class,method="selectWhitParam") @Results({ @Result(id=true,column="id",property="id"), @Result(column="CARD_ID",property="cardId"), @Result(column="POST_ID",property="postId"), @Result(column="QQ_NUM",property="qqNum"), @Result(column="BIRTHDAY",property="birthday",javaType=Date.class), @Result(column="CREATE_DATE",property="createDate",javaType=Date.class), /* * * 如果是EAGER,那么表示取出这条数据时,它关联的数据也同时取出放入内存中如果是LAZY, * 那么取出这条数据时,它关联的数据并不取出来,在同一个session中,什么时候要用,就什么时候取(再次访问数据库)。 * 但是,在session外,就不能再取了。用EAGER时,因为在内存里,所以在session外也可以取。 */ @Result(column="dept_id",property="dept",one=@One(select="csh.hrm.dao.DeptDao.findDeptById",fetchType=FetchType.EAGER)), @Result(column="job_id",property="job",one=@One(select="csh.hrm.dao.JobDao.findJobById",fetchType=FetchType.EAGER)), @Result(column="user_id",property="user",one=@One(select="csh.hrm.dao.UserDao.findUserById",fetchType=FetchType.EAGER)) }) List<Employee> selectWhitParam(Map<String, Object> params);
Emploee实体类:
private Integer id; // id private Dept dept; // 部门 private Job job; // 职位 private String name; // 名称 private String cardId; // 身份证 private String address; // 地址 private String postCode; // 邮政编码 private String tel; // 电话 private String phone; // 手机 private String qqNum; // qq private String email; // 邮箱 private Integer sex; // 性别 private String party; // 政治面貌 private User user;
日期形式的需要指定javaType=Date.class,不然MyBatis不知道该如何转换,一个员工属于一个部门,为一对一关系,一个员工有一个职位,也是一对一关系,数据库中Employee表中对应字段所存的为ID字段,例如员工对应一个部门,那么员工表中dept_id存放对应部门的ID,为外键,通过这个ID去对应的表中取得相应的数据,然后写入实体,csh.hrm.dao.DeptDao为部门对应的DAO,findDeptById为DAO类对应的方法,fetchType=FetchType.EAGER代码注解有解释
阅读全文
0 0
- MyBatis使用注解进行开发
- 使用注解开发 MyBatis
- Mybatis使用注解开发
- 准备使用MyBatis进行开发
- Mybatis之使用注解开发CRUD
- Mybatis之使用注解开发CRUD
- Mybatis之使用注解开发CRUD
- 如何使用AndroidStudio进行快速注解开发
- 使用mybatis进行jdbc的开发(实例)
- mybatis 注解开发
- MyBatis--注解式开发
- MyBatis--注解式开发
- Mybatis注解开发
- mybatis使用注解2
- mybatis使用注解2
- Mybatis使用 注解配置
- mybatis注解使用
- Spring使用AspectJ进行AOP的开发:注解方式
- 变态跳台阶
- 程序调试:mfcvc01 Microsoft基础类应用程序已停止工作
- Georgia and Bob POJ
- Spring Boot maven配置环境部署及定义相关属性
- Servlet向JSP传值
- MyBatis使用注解进行开发
- LightOJ-1012-Guilty Prince [DFS]
- 高斯混合模型GMM
- 《机器学习》读书笔记6
- IAT挂钩实现和检测
- 对表格的td标签执行点击事件
- Nim游戏—改 博弈论 解题报告
- JAVA多线程(第一天 )
- C编译过程以及gcc编译参数