加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Spring和mybatis的整合

发布时间:2020-12-15 01:52:06 所属栏目:大数据 来源:网络整理
导读:一、搭建项目开发环境 1. 新建一个maven项目SpringMybatis,项目结构如下: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 说明: src/main/java 存放java代码和映射文件: ? ? ? ? ?com.study.springmybatis.dao 存放m

一、搭建项目开发环境

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 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 org.mybatis mybatis 3.2.8 org.mybatis mybatis-spring 1.2.0 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 commons-logging commons-logging 1.1.3 junit junit 4.12 test

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里面的内容做对应的修改,修改后的内容如下

生成的pojo即实体类UserModel.java

.id = .username = username == ? .birthday = .sex = sex == ? .address = address == ? }

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

}
@Service("userService" UserService }

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

1. 数据库配置dbconfig.properties

validationQuery=SELECT 1

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

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

${driver} ${jdbc_url} ${jdbc_username} ${jdbc_password}

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

@RunWith(SpringJUnit4ClassRunner. @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/ 十月 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 十月 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读