Spring (four) --- Mybatis and Spring Integration

Step 1: Create a database

MySQL Code

 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');

 

Step 2: Create a Maven project

Import-related frame package

  • junit
  • mybaits
  • driving connection 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 is <dependency>
 39 <the groupId> org.mybatis </ the groupId>
 40 <the artifactId> MyBatis-Spring </ the artifactId>
 41 is <Version> 1.3.2 </ Version>
 42 is </ dependency>
 43 is </ Dependencies>
 44 is  
45 <Build>
 46 is <-! maven desirable when exporting the project, can be our configuration and resource exporter ->
 47 <resources>
 48 <resource>
 49 <Directory> the src / main / Java </ Directory>
 50 <Includes>
 51 is <the include> ** / * .properties </ the 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>

 

Step 3: Create entity classes

 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 }

 

Step Four: Create the interface and implementation class

Interface UserMapper

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

Implementation class 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>

 

Step Five: Configure mybatis file

Creating mybatis-config.xml file in the resources, simple to set up aliases and mapper mapping file

 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>

 

 Step Six: Write spring Profiles

Creating beans-config.xml file resources

1. Data source configuration

 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 Configuration

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 Configuration

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

 

In dao class created UserMapperImpl

4. adding an attribute interface class: 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. The interface implementation class to the spring to manage

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


Step Seven: Testing

 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 }

 

Step eight: Run results

 

Project structure

Guess you like

Origin www.cnblogs.com/tqsh/p/11302224.html