Spring和mybatis的整合

前端之家收集整理的这篇文章主要介绍了Spring和mybatis的整合前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、搭建项目开发环境

1. 新建一个maven项目SpringMybatis,项目结构如下:

                                                                               

说明:

src/main/java 存放java代码和映射文件

         com.study.springmybatis.dao 存放mapper接口

         com.study.springmybatis.mapper 存放mapper映射文件

         com.study.springmybatis.model 存放pojo类

         com.study.springmybatis.service 存放service接口和对应的实现类

src/test/java存放测试代码

src/main/resources 存放数据库配置文件和xml配置文件

2. 在pom.xml文件引入spring和mybatis相关的依赖,这里用的是阿里的maven远程仓库http://maven.aliyun.com/nexus/content/groups/public/

4.0.0 com.study.springmybatis SpringMybatis 0.0.1-SNAPSHOT jar SpringMybatis http://maven.apache.org UTF-8 添加Spring相关的依赖 begin org.springframework spring-core 4.3.12.RELEASE org.springframework spring-context 4.3.12.RELEASE org.springframework spring-tx 4.3.12.RELEASE org.springframework spring-jdbc 4.3.12.RELEASE org.springframework spring-test 4.3.12.RELEASE org.springframework spring-aop 4.3.12.RELEASE org.springframework spring-beans 4.3.12.RELEASE org.apache.geronimo.bundles aspectjweaver 1.6.8_2 添加Spring相关的依赖 end 添加mybatis的核心包 begin org.mybatis mybatis 3.2.8 添加mybatis的核心包 end 添加mybatis与Spring整合的核心包 begin org.mybatis mybatis-spring 1.2.0 添加mybatis与Spring整合的核心包 end 数据库连接相关包 begin org.wisdom-framework MysqL-connector-java 5.1.34_1 commons-dbcp commons-dbcp 1.4 commons-pool commons-pool 1.6 c3p0 c3p0 0.9.1.2 数据库连接相关包 end commons-logging commons-logging 1.1.3 junit junit 4.12 test

sql和使用mybatis逆向工程生成代码

1.建表

`id` () `username` () COMMENT 用户名称 `birthday` date COMMENT `sex` () COMMENT `address` () COMMENT ) ENGINEMyISAM AUTO_INCREMENT CHARSETutf8 COMMENT用户信息表;

2. 使用mybatis逆向工程生成代码

   mybatis逆向工程能根据数据库的表结构生成对应的mapper映射文件,接口,实体类,具体操作方法可以看我的前一篇文章,然后修改生成文件名称为符合java命名规范的名称

3. 生成逆向工程后的代码目录结构如下,这里使用的是spring的注解管理bean

   生成的mapper接口UserDao.java代码

用户接口 @Repository("userDao" }

生成的mapper映射文件mapper-user.xml,因为之前对生成的实体和接口做了名称修改,所以这里也要对mapper-user.xml里面的内容做对应的修改修改后的内容如下

sql sql

生成的pojo即实体类UserModel.java

用户信息表 用户名称 .id = .username = username == ? .birthday = .sex = sex == ? .address = address == ? }

业务的接口UserServiceI.java和实现类UserService.java

用户id获取用户 }
@Service("userService" UserService 自动注入UserDao }

三、在src/main/resources目录下编写配置文件

1. 数据库配置dbconfig.properties

MysqL.jdbc.Driver MysqL://192.168.152.1:3306/study?characterEncoding=utf-8 validationQuery=SELECT 1

2. Spring框架的核心配置文件spring.xml

配置文件 属性文件 自动扫描(自动注入),扫描com.study.springmybatis这个包以及它的子包的所有使用spring注解标注的类

3. Spring框架与Mybatis框架整合的配置文件

配置文件 ${driver} ${jdbc_url} ${jdbc_username} ${jdbc_password} 分隔线========================================= sqlSessionFactory sqlSessionFactory"sqlSessionfactorybean" sqlSessionFactory时需要使用上述配置好的数据源以及sql映射文件 自动扫描com/study/springmybatis/mapper/目录下的所有sql映射的xml文件,省掉sqlMapConfig.xml里的手工配置 文件 sql映射的xml文件位于com.study.springmybatis.mapper包下,这样就可以被自动扫描 sqlSessionfactorybeanName"sqlSessionFactory" 分隔线========================================= 拦截器方式配置事物 required" required" required" required" required" required" required" required" required" required" required"

四、在src/test/java编写测试类SpringMyBatisTest,本来测试类是放在src/test/java目录下面的。但是不知道为啥找不到junit的注解@Test,这里就偷了一下懒移到src/main/java目录的com.study.springmybatis.test包下面了

@RunWith(SpringJUnit4ClassRunner. 属性指明spring和配置文件之后, @ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mybatis.xml" userId = 1 UserModel user = System.out.println("查询到的用户名为:"+ }

运行测试类的结果为:

十月 22,2017 3:08:00 信息: Loaded TestExecutionListener names from location [Meta-INF/sqlScriptsTestExecutionListener] 十月 22,2017 3:08:00 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the listener classes (and their required dependencies) available. Offending : [javax/servlet/ 十月 22,2017 3:08:00 sqlScriptsTestExecutionListener@34ce8af7] 十月 22,2017 3:08:00 信息: Loading XML bean definitions from 十月 22,2017 3:08:00 信息: Loading XML bean definitions from path resource [spring- 十月 22,2017 3:08:00 信息: Refreshing org.springframework.context.support.GenericApplicationContext@5a61f5df: startup date [Sun Oct 22 15:08:00 CST 2017 查询到的用户名为:lgs

五、需要完整工程代码的同学去我的githup上下载https://github.com/leeSmall/SpringMybatis

猜你在找的Spring相关文章