本文适用于初学者:

该文主要教大家如何整合spring和mybatis,整合完成效果,可以从数据库中查询出学生信息:

img

完整的工程目录如下:

img

1|0整合思路:

  • 需要spring来管理数据源信息。
  • 需要spring通过单例方式管理SqlSessionFactory。
  • 使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
  • 持久层的mapper都需要由spring进行管理,spring和mybatis整合生成mapper代理对象。

下面开始工程搭建:

2|0第一步:创建工程 File—New—Project

img

img

img

img

点击Finish完成

注意:项目创建完成会有如下图提示:

img

这里是问你是否要自动导入,选择Enable即可,maven便会自动帮你导入jar包

3|0第二步:项目准备

本项目需要用到mysql数据库,首先先在mysql中创建一个数据库,然后创建一张表,sql语句如下:

CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `test`; /*Table structure for table `student` */ DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) DEFAULT NULL, `username` varchar(20) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, `email` varchar(20) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `student` */ insert into `student`(`id`,`username`,`password`,`email`) values (1,'jack','123','123@jack'), (2,'rose','520','rose@123');

4|0第三步:正式开发

4|11.修改pom文件

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.grandage</groupId> <artifactId>SpringMybatisTest</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- spring版本号 --> <spring.version>4.1.6.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!-- c3p0连接池jar --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> </dependencies> </project>

4|22.新建实体类Student:

package cn.grandage.pojo; public class Student { private Integer id; private String username; private String password; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "StudentMapper{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + '}'; } }

4|33.新建db.properties文件

配置文件放在resources目录下

db.driver=com.mysql.jdbc.Driver #数据库连接字符串(改成自己的连接) db.url=jdbc:mysql://localhost:3306/test #数据库用户名(这里改成自己的用户名) db.username=root #数据库密码(这里改成自己的密码) db.password=123456

4|44.新建StudentMapper接口

package cn.grandage.mapper; import cn.grandage.pojo.Student; public interface StudentMapper { public Student findStudentById(int id); }

4|55.新建mybatis映射配置文件:Student.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"> <!-- namespace:命名空间,对sql进行一个分类管理 --> <!-- 注意:namespace在mapper代理时,具有重要且特殊的作用 对应mapper接口的全限定名 --> <!--mybatis映射配置文件--> <mapper namespace="cn.grandage.mapper.StudentMapper"> <select id="findStudentById" parameterType="int" resultType="student"> select * from student where id=#{id} </select> </mapper>

4|66.新建mybatis核心配置文件:SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--mybatis核心配置文件--> <!--给类取别名,简化输入,方便映射配置文件中使用--> <typeAliases> <typeAlias type="cn.grandage.pojo.Student" alias="student"/> </typeAliases> <!--加载mapper映射配置文件--> <mappers> <mapper resource="Student.xml"/> </mappers> </configuration>

4|77.新建spring核心配置文件(applicationContext.xml),并整合mybatis:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!--引入java配置文件--> <context:property-placeholder location="db.properties"/> <!--配置数据源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> <property name="maxActive" value="10"/> <property name="maxIdle" value="5"/> </bean> <!--配置sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定Mybatis全局配置文件--> <property name="configLocation" value="classpath:SqlMapConfig.xml"/> <!--指定数据源--> <property name="dataSource" ref="dataSource"/> </bean> <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!--指定要映射的mapper接口的全限定名--> <property name="mapperInterface" value="cn.grandage.mapper.StudentMapper"/> <!--指定sqlSessionFactory--> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>

4|88.测试类编写:

import cn.grandage.mapper.StudentMapper; import cn.grandage.pojo.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test01 { @Test public void test01() { //获取applicationContext文件并加载 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //获取StudentDao的bean StudentMapper sd = (StudentMapper) ac.getBean("studentMapper"); Student s = sd.findStudentById(1); System.out.println("学生姓名:" + s.getUsername()); System.out.println("学生密码:" + s.getPassword()); System.out.println("学生邮箱:" + s.getEmail()); } }

4|9测试结果:

img


__EOF__

本文作者wugongzi
本文链接https://www.cnblogs.com/wugongzi/p/11726285.html
关于博主:我是说故事的五公子,欢迎扫描左边二维码关注
版权声明:沪漂一员,互联网架构方向,如有问题探讨可以直接私信我,亦可下方留言。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部