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
< grouppid > jUnit
jUnit
4.11
test
spring-context
spring-test
spring-test
org.springframework
spring-jdbc
spring-tx
spring-tx
c3p0
c3p0
0.9.1.2
org.mybatis
mybatis
3.4.1
< grouppid >org.mybatis
mybatis- Spring
mysql
mysql-connector-java
slf4j-log4j12
1.7.2
org.slf4j
slf4j-api
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 = “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)); }}