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,
com.xxx.service.impl
Test package: spring_mybatis
Importing 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://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>test-xxxms</artifactId>
<version>1.0 the SNAPSHOT</version>
<name>test-xxxms</name>
<! -- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<! -- Spring core JAR -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2. RELEASE</version>
</dependency>
<! -- Spring test jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2. RELEASE</version>
</dependency>
<! -- spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.2. RELEASE</version>
</dependency>
<! -- Spring things -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.2. RELEASE</version>
</dependency>
<! -- C3P0 connection pool -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<! -- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1 track</version>
</dependency>
<! -- Add mybatis with Spring core package -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<! Mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<! -- Jar related to log printing -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
<build>
<finalName>tpl-web</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
Copy the code
Configuring resource Files
A) The Spring file spring.xml
B) Mybatis file Mybatis. XML
C) Connect database to properties file db.properties
D) Log output file log4j.properties
Spring.xml file configuration
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="com.xxx"/>
<context:property-placeholder location="db.properties"/>
<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>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<! -- 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" />
</bean>
<! -- Configure the scanner -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<! Dao and all mapped interface classes under com.xxx.dao
<property name="basePackage" value="com.xxx.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>
Copy the code
Mybatis. XML file configuration
<! DOCTYPEconfiguration
PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<! -- Alias configuration everyone must know -->
<typeAliases>
<package name="com.xxx.model"/>
</typeAliases>
</configuration>
Copy the code
Db.properties 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
log4j.properties
Facilitates console log output
# 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
Copy the code
extension
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) {
this.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;
}
@Override
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 "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xxx.dao.UserDao">
<select id="queryUserById" parameterType="int" resultType="user">
select id,userName,userPwd from user where id=#{id}
</select>
</mapper>
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!
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserDao userDao;
public User queryUserById(a){
return userDao.queryUserById(7); }}Copy the code
Junit test requires video support materials or other information + our little sister V Lezijie007 (add friends code 98, no notes no add)
Because of integration with the Spring framework, we used the Spring framework to Test Spring tests
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"} )
public class TestSpringMybatis {
@Autowired
private UserService userService;
@Test
public void testQueryUserById(a) {
System.out.println(userService.queryUserById(1)); }}Copy the code