1, create the table, and insert the data:
CREATE TABLE ' alu '. ' T_user ' (
' user_id ' int (one) not null auto_increment,
' user_name ' char (= NOT null,
' User_password ' char not NULL,
PRIMARY key (' user_id '),
key ' idx_name ' (' user_name ')
Engine=innodb auto_increment=11 DEFAULT Charset=utf8
INSERT into Alu.t_user (user_id, user_name, User_password) VALUES (1, ' Luzhijian ', ' 123456 ');
2, add the dependency of the package:
<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.alu</groupId> <artifactid>mybatis_ Maven_project</artifactid> <version>0.0.1-SNAPSHOT</version> <properties> <!--Spring edition This number--> <spring.version>4.3.0.RELEASE</spring.version> <!--junit version number--> <junit.version>4. 12</junit.version> <!--mybatis version number--> <mybatis.version>3.2.1</mybatis.version> </proper Ties> <dependencies> <!--add Spring dependencies--> <dependency> <groupid>org.springframework& Lt;/groupid> <artifactId>spring-core</artifactId> <version>${spring.version}</version > </dependency> <dependency> <groupid>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version>${ spring.version}</version> </dependency> <dependency> <groupid>org.springframework</
Groupid> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>sprin G-context-support</artifactid> <version>${spring.version}</version> </dependency> < Dependency> <groupId>org.springframework</groupId> <artifactid>spring-aop</artifactid > <version>${spring.version}</version> </dependency> <dependency> <groupid>org. Springframework</groupid> <artifactId>spring-aspects</artifactId> <version>${ Spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version >${spring.version}</version> </dependency> <dependency> <groupid>org.springframework& Lt;/groupid> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid> ;spring-web</artifactid> <version>${spring.version}</version> </dependency> <!--unit test dependencies --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> < Version>${junit.version}</version> <scope>test</scope> </dependency> <!--spring Unit test Dependent--> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-test</ Artifactid> <VERSION>${SPRIng.version}</version> <scope>test</scope> </dependency> <!--mybatis dependent--> <DEP endency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <VERSION&G T;${mybatis.version}</version> </dependency> <!--mybatis/spring package--> <dependency> <g Roupid>org.mybatis</groupid> <artifactId>mybatis-spring</artifactId> <version>1.2.0 </version> </dependency> <!--mysql driver package--> <dependency> <groupid>mysql</groupid > <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </depende Ncy> </dependencies> </project>
3, configuration file:
3.1, add the Mybatis-config.xml file under the MyBatis package (MyBatis configuration file):
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en"
"HTTP://MYBATIS.ORG/DTD/MYBATIS-3-CONFIG.DTD" >
<configuration>
</configuration>
3.2. Add jdbc.properties file (data source configuration file) under Properties package:
Jdbc_driverclassname=com.mysql.jdbc.driver
Jdbc_url=jdbc:mysql://localhost:3306/alu
jdbc_username=root
Jdbc_password=luzhijian
3.3, Mapper Package usermapper.xml configuration file (table mapping file):
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en"
"HTTP://MYBATIS.ORG/DTD/MYBATIS-3-MAPPER.DTD" >
<mapper namespace= "Com.alu.dao.UserDao" >
<!--set field one by one for the domain class and tables in the database, noting that field names in database fields and domain classes do not. It must be here. -->
<resultmap id= "Baseresultmap" type= "Com.alu.domain.User" >
<id column= "user_id" UserId "jdbctype=" INTEGER/> <result column= "user_name"
property= "UserName" jdbctype= "CHAR"/>
<result column= "User_password" property= "UserPassword" jdbctype= "CHAR"/>
</resultMap>
<!-- Query single record-->
<select id= "Selectuserbyid" parametertype= "int" resultmap= "Baseresultmap" >
SELECT * From t_user WHERE user_id = #{userid}
</select>
</mapper>
3.4. Spring configuration file Application.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "http://www.springframework.org/sc Hema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springfra MEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.spri
Ngframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--introduce JDBC profile--> <bean id= "Propertyconfigurer class=" ORG.SPRINGFRAMEWORK.BEANS.FACTORY.CONFIG.P Ropertyplaceholderconfigurer "> <property name=" Locations "> <list> <va Lue>classpath:properties/*.properties</value> <!--If you have more than one profile, just go ahead and add--> </list> </property> &L T;/bean> <!--configuration data source--> <bean id= "DataSource" class= Managerdatasource "> <!--do not use properties to configure--> <!--<property name=" Driverclassname "value=" COM.MYSQL.J Dbc. Driver "/> <property name=" url "value=" jdbc:mysql://localhost:3306/learning "/> <property" name= Me "value=" root "/> <property name=" password "value=" christmas258@ "/>--> <!--use properties to configure--&
Gt <property name= "Driverclassname" > <value>${jdbc_driverClassName}</value> </property> <p
Roperty name= "url" > <value>${jdbc_url}</value> </property> <property name= "username" > <value>${jdbc_username}</value> </property> <property name= "password" > <value>${jdbc _password}</vAlue> </property> </bean> <!-- Automatically scans all the Xxxxmapper.xml corresponding mapper interface files, so that you do not need a manual configuration Mpper mapping, as long as the Mapper interface class and mapper mapping file corresponding to it. --> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "basepackage" value = "Com.alu.dao"/> </bean> <!--configure MyBatis files, mapperlocations Configure **mapper.xml file locations, configlocation configure mybatis- Config file location--> <bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property
Name= "DataSource" ref= "DataSource"/> <property name= "mapperlocations" value= "Classpath:mapper/*.xml"/> <property name= "configlocation" value= "Classpath:mybatis/mybatis-config.xml"/> <!--<property name= "Typ Ealiasespackage "value=" Com.tiantian.ckeditor.model/>--> </bean> <!--automatically scans annotations for Bean--> <con
Text:component-scan base-package= "Com.alu.service"/> </beans>
4, interface and the configuration of the class:
4.1. Add User.java files under Com.luo.domain:
Package com.alu.domain;
public class User {
private Integer userId;
Private String userName;
Private String UserPassword;
Public Integer GetUserID () {return
userId;
}
public void Setuserid (Integer userId) {
This.userid = userId;
}
Public String GetUserName () {return
userName;
}
public void Setusername (String userName) {
this.username = userName;
}
Public String Getuserpassword () {return
userpassword;
}
public void SetUserPassword (String userpassword) {
This.userpassword = UserPassword;
}
}
4.2. Add Userdao.java files under Com.luo.dao:
Package Com.alu.dao;
Import Com.alu.domain.User;
Public interface Userdao {
/**
* @param userId
* @return User
*
/Public user Selectuserbyid (Integer userId);
}
4.3. Add Userservice.java interface and Userserviceimpl implementation class under Com.luo.service:
Package com.alu.service;
Import Com.alu.domain.User;
Public interface UserService {
User Selectuserbyid (Integer userId);
}
Package com.alu.service;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
Import Com.alu.dao.UserDao;
Import Com.alu.domain.User;
@Service public
class Userserviceimpl implements UserService {
@Autowired
private Userdao Userdao;
Public User Selectuserbyid (Integer userId) {return
Userdao.selectuserbyid (userId);
}
}
5. Unit Test
5.1. Add Springtestcase.java under Com.luo.baseTest:
Package com.alu.baseTest;
Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Specifies @ContextConfiguration of the configuration file injected by the bean
(locations = {"Classpath:application.xml"})
//Use standard JUnit @ Runwith comment to tell JUnit to use the spring Testrunner
@RunWith (springjunit4classrunner.class) public
class Springtestcase Extends Abstractjunit4springcontexttests {
}
5.2, Com.luo.service Add Userservicetest.java:
Package com.alu.service;
Import Org.junit.Test;
Import org.springframework.beans.factory.annotation.Autowired;
Import Com.alu.baseTest.SpringTestCase;
Import Com.alu.domain.User;
Import Com.alu.service.UserService;
public class Userservicetest extends Springtestcase {
@Autowired
private userservice userservice;
@Test public
void Selectuserbyidtest () {
User user = Userservice.selectuserbyid (1);
System.out.println (User.getusername () + ":" + User.getuserpassword ());
}
5.3, run unit test, userservicetest right key run As–>junit test, run results: