The way of integration

  • Create a new Maven project
  • Introducing dependency packages
  • Configuration resource file

A case in field

Create 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,

com.xxx.service.impl

Test package: spring_mybatis

Introducing dependency packages

Open pom.xml and start adding dependency packages

< project XMLNS = “http://maven.apache.org/POM/4.0.0” XMLNS: xsi = “http://www.w3.org/2001/XMLSchema-instance” Xsi: schemaLocation = “HTTP: / / http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m… D “> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. XXX < / groupId > < artifactId > test – XXXMS < / artifactId > < version > 1.0 – the SNAPSHOT < / version > < name > test – XXXMS < / name >


http://www.example.com
< project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < maven.com piler. Source > 1.7 < / maven.com piler source > < maven.piler.target >1.7

< Dependencies >

< grouppid > jUnit


jUnit

4.11

test

spring-context
. Spring Framework spring-context. Spring Framework spring-context < version > 4.3.2. RELEASE < / version > < / dependency >


spring-test
. Spring Framework


spring-test
< version > 4.3.2. RELEASE < / version > < / dependency >


org.springframework

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


spring-tx
. Spring Framework

spring-tx
< version > 4.3.2. RELEASE < / version > < / dependency >


c3p0

c3p0

0.9.1.2

org.mybatis

mybatis

3.4.1

< grouppid >org.mybatis


mybatis- Spring
The < version > 1.3.0 < / version > < / dependency >


mysql

mysql-connector-java
The < version > 5.1.39 < / version > < / dependency >


slf4j-log4j12

1.7.2

org.slf4j

slf4j-api
1.7.2


TPL-web

src/main/java

*/.xml

src/main/resources

*/.xml

*/.properties

Configuration resource file

A) Spring file spring.xml

B) MyBatis file myBatis. XML

C) Database connection properties file db.properties

D) Log output file log4j.properties

The spring.xml file configuration

< bean id = “dataSource” class = “boPooledDataSource com.mchange.v2.c3p0.Com” > < property name = “driverClass” value=”${jdbc.driver}”> < bean id = “sqlSessionFactory” class = “org. Mybatis. Spring. SqlSessionFactoryBean” >

< property name = “mapperLocations” value = “classpath: com/XXX/dao/mapper / *. XML” / > < / bean >
< bean id = “mapperScanner” class = “org. Mybatis. Spring. Mapper. MapperScannerConfigurer” >

Mybatis. XML file configuration

Db.properties file configuration (for other data source property configuration, see the C3P0 configuration tutorial, where the default property configuration is used)

Establish database MyBatis (Note that the database, user name and password are subject to your local database)

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

log4j.properties

Console log output is easy

Global logging configuration

log4j.rootLogger=DEBUG, stdout

Console output…

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] – %m%n

extension

Start writing HelloWorld

User entity class definition

public class User {



private int id;



private String userName;



private String userPwd;



public int getId() {



return id;



}



public void setId(int id) {



this.id = id;



}



public String getUserName() {



return userName;



}



public void setUserName(String userName) {



this.userName = userName;



}



public String getUserPwd() {



return userPwd;



}



public void setUserPwd(String userPwd) {



this.userPwd = userPwd;



}



@Override



public String toString() {



return “User [id=” + id + “, userName=” + userName + “, userPwd=”

  • userPwd + “]”;

}}

UseDao interface and mapping file definition

UserDao interface

public interface UserDao {



public User queryUserById(int id);



}

Usermapper. XML (Note: The mapping file namespace definition should follow 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 a circle!!

< mapper namespace=”com.xxx.dao.UserDao”>

UserService interface class and implementation class definition

public interface UserService {



public User queryUserById();



}

UserServiceImpl implementation class (directly inject our UserDAO interface at this point, and then call it directly

Its method, the matter is here, only one step away from success!

@Service public class UserServiceImpl implements UserService{ @Resource private UserDao userDao; public User queryUserById(){ return userDao.queryUserById(7); }}

Junit tests

Because of the integration with the Spring Framework, we used the Spring Framework to Test the Spring Test

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {“classpath:spring.xml”} ) public class TestSpringMybatis { @Autowired private UserService userService; @Test public void testQueryUserById() { System.out.println(userService.queryUserById(1)); }}

Results output