Spring---Mybatis和Spring整合

Posted tqsh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring---Mybatis和Spring整合相关的知识,希望对你有一定的参考价值。

第一步:创建数据库

mysql代码

 1 CREATE DATABASE `mybatis` ;
 2 
 3 USE `mybatis`;
 4 
 5 CREATE TABLE `user` (
 6 `id` INT(20) NOT NULL,
 7 `name` VARCHAR(30) DEFAULT NULL,
 8 `pwd` VARCHAR(30) DEFAULT NULL,
 9 PRIMARY KEY (`id`)
10 ) ENGINE=INNODB DEFAULT CHARSET=utf8;
11 
12 INSERT INTO `user`(`id`,`name`,`pwd`) 
13 VALUES (1,‘闪电侠‘,‘123‘),(2,‘蜘蛛侠‘,‘abc‘),(3,‘钢铁侠‘,‘520‘);

 

第二步:创建一个Maven项目

导入相关架包

  • junit
  • mybaits
  • mysql连接驱动
  • Spring
  • spring-jdbc
  • mybatis-spring
 1 <dependencies>
 2 <!--junit 单元测试-->
 3 <dependency>
 4 <groupId>junit</groupId>
 5 <artifactId>junit</artifactId>
 6 <version>4.11</version>
 7 </dependency>
 8 
 9 <!--mybaits-->
10 <dependency>
11 <groupId>org.mybatis</groupId>
12 <artifactId>mybatis</artifactId>
13 <version>3.4.6</version>
14 </dependency>
15 
16 <!--mysql连接驱动-->
17 <dependency>
18 <groupId>mysql</groupId>
19 <artifactId>mysql-connector-java</artifactId>
20 <version>5.1.47</version>
21 </dependency>
22 
23 <!--Spring的包-->
24 <dependency>
25 <groupId>org.springframework</groupId>
26 <artifactId>spring-webmvc</artifactId>
27 <version>4.3.9.RELEASE</version>
28 </dependency>
29 
30 <!--spring-jdbc -->
31 <dependency>
32 <groupId>org.springframework</groupId>
33 <artifactId>spring-jdbc</artifactId>
34 <version>4.3.9.RELEASE</version>
35 </dependency>
36 
37 <!--mybatis-spring-->
38 <dependency>
39 <groupId>org.mybatis</groupId>
40 <artifactId>mybatis-spring</artifactId>
41 <version>1.3.2</version>
42 </dependency>
43 </dependencies>
44 
45 <build>
46 <!--希望maven在导出项目的时候,能够将我们的配置及资源导出-->
47 <resources>
48 <resource>
49 <directory>src/main/java</directory>
50 <includes>
51 <include>**/*.properties</include>
52 <include>**/*.xml</include>
53 </includes>
54 <filtering>false</filtering>
55 </resource>
56 <resource>
57 <directory>src/main/resources</directory>
58 <includes>
59 <include>**/*.properties</include>
60 <include>**/*.xml</include>
61 </includes>
62 <filtering>false</filtering>
63 </resource>
64 </resources>
65 </build>

 

第三步:创建实体类

 1 public class User 
 2 
 3 private int id;
 4 private String name;
 5 private String pwd;
 6 
 7 public User() 
 8 
 9 
10 public User(int id, String name, String pwd) 
11 this.id = id;
12 this.name = name;
13 this.pwd = pwd;
14 
15 
16 public int getId() 
17 return id;
18 
19 
20 public void setId(int id) 
21 this.id = id;
22 
23 
24 public String getName() 
25 return name;
26 
27 
28 public void setName(String name) 
29 this.name = name;
30 
31 
32 public String getPwd() 
33 return pwd;
34 
35 
36 public void setPwd(String pwd) 
37 this.pwd = pwd;
38 
39 
40 @Override
41 public String toString() 
42 return "User" +
43 "id=" + id +
44 ", name=‘" + name + ‘\\‘‘ +
45 ", pwd=" + pwd +
46 ‘‘;
47 
48 

 

第四步:创建接口和实现类

接口UserMapper

1 public interface UserMapper 
2 
3 List<User> getUser();
4 
5 

实现类UserMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <!--mapper标签的namespace对应Mapper接口的类-->
 7 <mapper namespace="com.shandx.dao.UserMapper">
 8 
 9 <select id="getUser" resultType="User">
10 select * from user
11 </select>
12 
13 </mapper>

 

第五步:配置mybatis文件

在resources下创建mybatis-config.xml文件,简单的设置别名和mapper映射文件

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7 <!--别名-->
 8 <typeAliases>
 9 <package name="com.shandx.pojo"/>
10 </typeAliases>
11 
12 <!--关联映射文件-->
13 <mappers>
14 <mapper resource="com/shandx/dao/UserMapper.xml"/>
15 </mappers>
16 
17 </configuration>

 

 第六步:编写spring配置文件

在resources创建beans-config.xml文件

1.数据源配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xsi:schemaLocation="http://www.springframework.org/schema/beans
 5 http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7 <!--1.配置数据源-->
 8 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 9 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
10 <property name="url" value="jdbc:mysql://localhost:3306/mybatis?
11 useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf-8"/>
12 <property name="username" value="root"/>
13 <property name="password" value="123456"/>
14 </bean>
15 
16 </beans>

2.sqlSessionFactory配置

1 <!--2.配置SqlSessionFactory-->
2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3 <property name="dataSource" ref="dataSource"/>
4 <!--关联mybatis的配置文件-->
5 <property name="configLocation" value="classpath:mybatis-config.xml"/>
6 </bean>

3.sqlSession配置

1 <!--3.创建SqlSession-->
2 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
3 <constructor-arg index="0" ref="sqlSessionFactory" />
4 </bean>

 

在dao类下创建UserMapperImpl

4.接口实现类增加一个属性:sqlSession

 1 public class UserMapperImpl implements UserMapper 
 2 
 3 private SqlSessionTemplate sqlSession;
 4 
 5 public void setSqlSession(SqlSessionTemplate sqlSession) 
 6 this.sqlSession = sqlSession;
 7 
 8 
 9 public List<User> getUser() 
10 
11 UserMapper mapper = sqlSession.getMapper(UserMapper.class);
12 return mapper.getUser();
13 
14 
15 

5.将接口的实现类交给spring来管理

1 <!--4.接口实现类注入SqlSession-->
2 <bean id="UserMapperImpl" class="com.shandx.dao.UserMapperImpl">
3 <property name="sqlSession" ref="sqlSession"/>
4 </bean>


第七步:测试

 1 public class UserMapperTest 
 2 
 3 @Test
 4 public void getUser()
 5 ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
 6 UserMapper userDaoImpl = (UserMapper) context.getBean("UserMapperImpl");
 7 List<User> users = userDaoImpl.getUser();
 8 for (User user : users) 
 9 System.out.println(user);
10 
11 
12 

 

第八步:运行结果

技术图片

 

项目结构

技术图片

以上是关于Spring---Mybatis和Spring整合的主要内容,如果未能解决你的问题,请参考以下文章

spring+Mybatis 整合报错

spring整合Mybatis

spring,mybatis 整合 启用tomcat 报错

Spring——MyBatis整合

SSM框架整合Spring,Mybatis

SSM框架整合Spring,Mybatis