Mybatis 与 spring 的融合:今天给大家分享下自己在搭建 mybatis+spring 过程中的一些心得,众所周知,mybatis 是数据库访问的框架,spring 是另一个 java 的轻量级框架。
spring 提供强大的基于 javaBeans 的采用控制反转(Inversion of Control,IoC)原则的配置管理,使得应用程序的组件更加快捷简易。
话不多说,配置如下:
- junit junit 4.10 org.apache.commons commons-lang3 3.4 ch.qos.logback logback-access 1.1.8 ch.qos.logback logback-classic 1.1.8 ch.qos.logback logback-core 1.1.8 org.slf4j slf4j-api 1.7.22 com.alibaba fastjson 1.2.4 aopalliance aopalliance 1.0 commons-logging commons-logging 1.1.1 org.mybatis mybatis 3.4.0 org.mybatis mybatis-spring 1.3.0 mysql mysql-connector-java 5.1.38 org.springframework spring-aop 4.1.6.RELEASE org.springframework spring-beans 4.2.5.RELEASE org.springframework spring-context 4.2.5.RELEASE org.springframework spring-core 4.2.6.RELEASE org.springframework spring-jdbc 4.2.6.RELEASE org.springframework spring-tx 4.2.6.RELEASE c3p0 c3p0 0.9.1.2 commons-collections commons-collections 3.2.2 org.apache.directory.studio org.apache.commons.codec 1.3
以上是配置中所用到的相关的 jar 包,项目整理的结构如图:
本项目中主要的配置文件是数据库的连接配置、日志记录配置、mybatis.xml(用于设置别名、以及一些 plugins) 以及 spring-mybatis.xml。
jdbc.properties 配置如下:
- jdbc.driverClassName = com.mysql.jdbc.Driverjdbc.username = rootjdbc.password = 123456jdbc.maxPoolSize = 20jdbc.minPoolSize = 1jdbc.initialPoolSize = 1jdbc.idleConnectionTestPeriod = 1800jdbc.maxIdleTime = 3600jdbc.checkoutTimeout = 10000jdbc.url = jdbc: mysql: //{ip}:{port}/{db-name}?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true&socketTimeout=100000&connectTimeout=10000
logback.xml 主要是用于日志记录的,相关的配置如下:
- % d {
- yyyy - MM - dd HH: mm: ss.SSS
- } [ % thread] % -5level % logger {
- 50
- } [ % file: %line] - %msg % n $ {
- LOG_HOME
- }
- /dsp-geoedge.log.%d{yyyy-MM-dd}.log 30 %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} [%file:%line] - %msg%n 10MB /
mybatis.xml 中的配置主要是定义一些 model 中的别名,方便在 mapper.xml 中使用。相关的配置如下:
下面就是本文最为关键的配置文件了,spring-mybatis.xml 的配置:
userDao 代码:
- public interface UserDao {
- User getUser(Long id);
- }
IUserService 代码:
- public interface IUserService {
- User getUser(Long id);
- }
User 类:
- public class User {
- private Long id;
- private String name;
- private String email;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
UserServiceImpl 代码:
- @Service("userService") public class UserServiceImpl extends BaseLog implements IUserService {@Resource private UserDao userDao;
- public User getUser(Long id) {
- return userDao.getUser(id);
- }
- }
相关的 test 类的具体调用代码:
- ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
- UserServiceImpl service = (UserServiceImpl) context.getBean("userService");
- User user = service.getUser(3l);
- assertNotNull(user);
- assertEquals("alven", user.getName());
相关配置就已经全部介绍完毕,同时期待大神指点指点,对本文的配置提出优化方案,谢谢大家。
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: