上一章Spring集成MyBatis进行项目开发(一)已经介绍了spring3和mybatis整合的配置,接下来是一个项目里面的部分代码:
Application.java是下面会用到的一个实体bean:
public class Application { public static final int APP_DISABLE = 0; public static final int APP_ENABLE = 1; private Integer id; private String appAccount;//每个app对应一个账户标识;对应生成的数据表 private String appName; private String appICON; private String appDesc; private String appURL; private Date createTime; private int isDisable;//'是否前台显示:0显示,1不显示' }
getter 和setter略。
首先我们要编写一个与mapper.xml文件映射的接口文件,mybatis会将这个接口文件和对应的mapper文件中的sql语句关联,自动实现这个接口文件。在之后的开发中我们直接调用这个接口文件就可以了,因为内存中已经有接口相对应的实例了。
ApplicationsMapper.xml文件:
<?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="com.pinche.statistic.mapper.ApplicationsMapper"> <insert id="add" parameterType="Application" useGeneratedKeys="true" keyProperty="id"> INSERT INTO applications (appName,appAccount,appICON,appDesc,appURL,createTime) VALUES (#{appName},#{appAccount},#{appICON},#{appDesc},#{appURL},#{createTime}) </insert> <delete id="delete" parameterType="String"> DELETE FROM applications WHERE appAccount = #{appAccount} </delete> <update id="update" parameterType="Application"> UPDATE applications <set> <if test=" appName != '' and appName != null "> appName = #{appName}, </if> <if test=" appICON != '' and appICON != null "> appICON = #{appICON}, </if> <if test=" appDesc != '' and appDesc != null "> appDesc = #{appDesc}, </if> <if test=" appURL != '' and appURL != null "> appURL = #{appURL}, </if> <if test=" isDisable != -1 "> isDisable = #{isDisable} </if> </set> WHERE appAccount = #{appAccount} </update> <select id="findByAppAccount" resultType="Application" parameterType="String"> select * from applications where appAccount = #{appAccount} </select> <select id="findAll" resultType="Application"> select * from applications </select> </mapper>
对ApplicationsMapper.xml文件的配置必须要注意的是它的命名空间是必须的,而且是对应接口文件的全名!并且每个sql语句的id属性和接口文件中的方法名一致!!
下面是ApplicationsMapper.java文件,也就是对应的接口文件:
package com.pinche.statistic.mapper; import java.util.List; import com.pinche.statistic.domain.Application; public interface ApplicationsMapper { void add(Application app); void delete(String appAccount); void update(Application app); Application findByAppAccount(String appAccount); List<Application> findAll(); }
通过以上的的配置,大家可以在test中测试一下自己的代码了:
@Test public void testCreateTable() { ApplicationContext aContext = new FileSystemXmlApplicationContext("src/main/webapp/WEB-INF/applicationContext.xml"); ApplicationsMapper mapper = (ApplicationsMapper) aContext.getBean(ApplicationsMapper.class); Application app = new Application(); app.setAppAccount("androidApp"); mapper.add(app); }
以上测试了add方法,其他的测试方法大家可以照着写一写。如果插入成功恭喜,你的环境已经搭好了。
接下来是将继续这个样例系统的Dao层,service层和controller层。
这个dao层吧,项目中就是这么用的,不过现在通过大家的指正,mybatis提供了@param来解决多参数问题,ok,这么看来这个Dao层的确不需要了。
AppDao.java:
package com.pinche.statistic.dao; import java.util.List; import com.pinche.statistic.domain.Application; public interface AppDao { boolean add(Application app); boolean delete(String appAccount); boolean update(Application app); Application findByAppAccount(String appAccount); List<Application> findAll(); }
对应的实现类AppDaoImpl.java
package com.pinche.statistic.dao.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Repository; import com.pinche.statistic.dao.AppDao; import com.pinche.statistic.domain.Application; import com.pinche.statistic.mapper.ApplicationsMapper; @Repository public class AppDaoImpl implements AppDao { @Autowired private ApplicationsMapper mapper; @Override public boolean add(Application app) { try { mapper.add(app); return true; } catch (DataAccessException e) { e.printStackTrace(); } return false; } @Override public boolean delete(String appAccount) { try { mapper.delete(appAccount); return true; } catch (DataAccessException e) { e.printStackTrace(); } return false; } @Override public boolean update(Application app) { try { mapper.update(app); return true; } catch (DataAccessException e) { e.printStackTrace(); } return false; } @Override public Application findByAppAccount(String appAccount) { try { Application findByAppAccount = mapper.findByAppAccount(appAccount); return findByAppAccount; } catch (DataAccessException e) { e.printStackTrace(); } return null; } @Override public List<Application> findAll() { try { return mapper.findAll(); } catch (DataAccessException e) { e.printStackTrace(); } return null; } }
自行设计的DAO层对象容器(在DAO对象很多时,如果在service层要调用对应的DAO还得手动注入,通过引用这个DAO层对象容器,可以实现在需要使用DAO时迅速找需要的DAO,省去了繁杂的手动注入,而且spring默认的bean都是单例的,无论在何处注入一个实体bean其实都是同一个。这样做更方便):
package com.pinche.statistic.dao; import java.lang.reflect.Field; import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class BaseDAL { private static final Logger logger = LoggerFactory.getLogger(BaseDAL.class); @Autowired private AppDao _appDao; public static AppDao appDao; @Autowired private MetaDataDao _metaDataDao; public static MetaDataDao metaDataDao; @Autowired private DDLManager _DDLManager; public static DDLManager DDLManager; @Autowired private AnalyzeDao _analyzeDao; public static AnalyzeDao analyzeDao; @Autowired private DialstatisticsDao _dialstatisticsDao; public static DialstatisticsDao dialstatisticsDao; @PostConstruct public void init() { long start = System.currentTimeMillis(); logger.debug("start init BaseDAL ..."); try { Field[] fields = this.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { String fieldname = fields[i].getName(); if (fieldname.startsWith("_")) { String sfieldname = fieldname.substring(1); Field sfield = this.getClass().getDeclaredField(sfieldname); sfield.setAccessible(true); sfield.set(this, fields[i].get(this)); } } logger.debug("init BaseDAL OVER, consume = {}ms", System.currentTimeMillis() - start); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
如果使用了以上的层管理容器,如果要在容器中添加一个DAO(例如:DemoDao),只需在这个容器中添加一个这样的声明:
@Autowired private DemoDao _demoDao; public static DemoDao demoDao;
相关推荐
SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+Mybatis集成开发环境SpringMVC+Spring+...
Spring集成Mybatis所需jar包
SpringMVC+Spring+Mybatis集成开发环境
spring集成mybatis,资源包括: 中文版MyBatis 3 User Guide.pdf mybatis-spring集成指导.pdf Mybatis.pdf mybatis-3.2.8.jar mybatis-spring-1.2.2.jar
spring mvc mybatis 整合源码,带数据库脚本,带详细注释,适合初、中、高各级开发人员使用
NULL 博文链接:https://fangyong1307.iteye.com/blog/2276550
Spring Boot 集成mybatis,Spring Boot和mybatis整合的开发
spring4.0 集成 tk.mybatis插件,增删改无需增删改查,快速开发
1. 该框架的开发环境为EclipseJEE集成开发工具。 2. 基于Spring4框架的三层架构开发完成。 3. Mybatis版本为:3.2.8。 4. 使用了SpringMVC框架,Spring4的xml配置和注解配置。 5. 前台展示页面使用了JQuery ...
本套源代码实现了Spring 和 mybatis 和 layui 集成情况下的基础操作,包括: 增删改查、分页、导入导出等功能。数据库为mysql,脚本在源代码根目录下,mybatis_crud.sql
springMVC(4.1.2)+spring(4.1.2)+Mybatis+maven整合,项目部署时请配好开发环境!
Windchill+spring+mybatis 1、修改codebase/MVCDispatcher-servlet.xml新增如下内容,配置mybatis-spring
基于 SpringBoot 从0搭建一个企业级开发项目,基于SpringBoot 的项目,并集成MyBatis-Plus、Redis、Druid、Logback ,并使用 Redis 配置 MyBatis 二级缓存。
完美商务管理系统:采用经典搭配SSM技术架构,功能非常全,代码完整度非常高,其中集成了非常多的插件技术,聊天可用,图片裁剪、上传、图片采集、调用摄像头,按钮开关(UI体验...Spring+Springmvc+Mybatis+ace+jquery
项目概述:本项目深入整合Spring Data与MyBatis,致力于简化基于数据访问技术的Spring应用程序开发流程。共计296个文件,主要包括200个Java源码文件,以及39个VM模板、19个XML配置、7个SQL脚本、4个Markdown文档、2...
springmvc+spring+mybatis集成开发demo eclipse项目 具体事项请阅读SSIDemo/WebContent/readme.txt 数据库文件在SSIDemo/si.sql
最近帮一个师弟写的一个简单系统,虽小,但是想学习三大框架集成的可以下载来看看
本压缩文件为spring,springMVC,myBatis集成开发源码,解压后可直接在eclipse中使用,有问题也可留言;
提供事务管理:Spring提供了强大的事务管理功能,可以与MyBatis无缝集成。通过整合,可以使用Spring事务管理器对数据库操作进行事务控制,实现数据一致性和事务边界的管理。 注解驱动的开发:MyBatis支持使用注解来...