Integrated approach

  • Creating a New Maven Project

  • Importing dependency packages

  • Configuring resource Files

A case in field

Creating a New Maven Project

Create a new Maven project spring_mybatis

The directory structure is as follows:

Home directory package:

Com. XXX. Dao,

Com. XXX. Mapper,

Com. XXX. The service,


Test package: spring_mybatis

Importing dependency packages

Open pom.xml and start adding dependency packages


<project xmlns="" xmlns:xsi=""

  <version>1.0 the SNAPSHOT</version>

  <! -- FIXME change it to the project's website -->



    <! -- Spring core JAR -->
      <version>4.3.2. RELEASE</version>

    <! -- Spring test jar -->
      <version>4.3.2. RELEASE</version>

    <! -- spring jdbc -->
      <version>4.3.2. RELEASE</version>

    <! -- Spring things -->
      <version>4.3.2. RELEASE</version>

    <! -- C3P0 connection pool -->

    <! -- mybatis -->
      <version>3.4.1 track</version>

    <! -- Add mybatis with Spring core package -->

    <! Mysql -->

    <! -- Jar related to log printing -->



Copy the code

Configuring resource Files

A) The Spring file spring.xml

B) Mybatis file Mybatis. XML

C) Connect database to properties file

D) Log output file

Spring.xml file configuration

<beans xmlns=""

    <context:component-scan base-package=""/>

    <context:property-placeholder location=""/>

    <tx:annotation-driven  transaction-manager="txManager"/>

    <! C3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>

    <bean id="txManager"
        <property name="dataSource" ref="dataSource"></property>

<! -- Integration framework (Spring and Mybatis)

    <! - the configuration sqlSessionFactory -- -- >
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <! -- Data source -->
        <property name="dataSource" ref="dataSource"></property>
        <! -- Framework configuration file -->
        <property name="configLocation" value="classpath:mybatis.xml" />
        <! -- Mapping file -->
        <property name="mapperLocations" value="classpath:com/xxx/dao/mapper/*.xml" />

    <! -- Configure the scanner -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <! Dao and all mapped interface classes under
        <property name="basePackage" value="" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

Copy the code

Mybatis. XML file configuration

<! DOCTYPEconfiguration
        PUBLIC "- / / Config / 3.0 / EN"


    <! -- Alias configuration everyone must know -->
        <package name=""/>

Copy the code file configuration (for other data source property configuration, see c3P0 configuration description, where default property configuration is used)

Establish database mybatis (note database, user name, password is subject to their own local database)

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis? useUnicode=true&characterEncoding= utf8 jdbc.username=root jdbc.password=Copy the code

Facilitates console log output

# Global logging configuration 
log4j.rootLogger=DEBUG, stdout 
# Console output... 
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 
Copy the code


Start writing HelloWorld

User entity class definition

public class User { 

    private int id; 

    private String userName; 

    private String userPwd; 

    public int getId(a) { 

    	return id; 


    public void setId(int id) { = id; 


    public String getUserName(a) { 

    	return userName; 


    public void setUserName(String userName) { 

    	this.userName = userName; 


    public String getUserPwd(a) { 

    	return userPwd; 


    public void setUserPwd(String userPwd) { 

    	this.userPwd = userPwd; 



    public String toString(a) { 

    	return "User [id=" + id + ", userName=" + userName + ", userPwd=" 

    \+ userPwd + "]"; }}Copy the code

UseDao interface and mapping file definition

UserDao interface

public interface UserDao { 

	public User queryUserById(int id); 

Copy the code

Usermapper. XML (Note: the mapping file namespace definition should comply with the rule: interface package name. Interface class

Name, otherwise not according to the rules of the card, the test will report an error, and then you will be confused!!


<! DOCTYPEmapper 

PUBLIC "- / / Mapper / 3.0 / EN" 


<mapper namespace=""> 

<select id="queryUserById" parameterType="int" resultType="user"> 

	select id,userName,userPwd from user where id=#{id}  


Copy the code

UserService interface class and implementation class definition

public interface UserService { 

	public User queryUserById(a); 

Copy the code

UserServiceImpl implementation class (at this point just inject our UserDao interface and call it directly

Its method, the matter has been so far, only one step away from success!


public class UserServiceImpl implements UserService{ 


    private UserDao userDao; 

    public User queryUserById(a){ 

    	return userDao.queryUserById(7); }}Copy the code

Junit tests

Because of integration with the Spring framework, we used the Spring framework to Test Spring tests


@ContextConfiguration(locations = {"classpath:spring.xml"} ) 

public class TestSpringMybatis { 


    private UserService userService; 


    public void testQueryUserById(a) { 

    	System.out.println(userService.queryUserById(1)); }}Copy the code

Results output