Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
This article is to implement a Web project using Mybatis+spring+mysql to achieve the whole goal. Configure the sqlsessionfactory of the data source and MyBatis in spring, and then get the beans in spring in the JSP in the Web. Use this bean to manipulate tables in MySQL. Online read a lot of people have written, but either the figure is not clear, or the code is not finished. This is a complete tutorial and you can integrate it yourself!
Version used: Mybatis-3.2.8
Spring3.2.9
Mysql5.6
Development environment: Eclipse Java EE kepler+win7
This article works free download
Create a Web project and import the package
Create a Web project in Eclipse and import the following package:
Here must be careful not to mybatis-spring-1.2.2.jar this package, this package is used to contact spring and mybatis, very important!!
The entire project directory is as follows:
Second, create the Library table mapping class and configure the MyBatis
1. First create a table in the database T_user
Use Test;drop table IF EXISTS t_user;create table T_user (userId int primary key auto_increment, UserName VARCHAR (50) Not NULL, Userage int is not NULL);
then insert 4 data:
INSERT into T_user values (1, ' Xiao Wang ', +), insert into t_user values (2, ' Red ', one-to-one), insert into T_user values (3, ' plainly ', '); insert Into T_user values (4, ' daily ', 13);
View the results below:
2, the table created after the creation of its mapping class user, in the package Com.mucfc.model
Package com.mucfc.model;/** * User Mapping class * @author Linbingwen * @time 2015.5.15 */public class User {private Integer userid;p rivate String username;private int userage;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 int getuserage () {return userage;} public void setuserage (int userage) {this.userage = Userage;} @Overridepublic String toString () {return "User [userid=" + UserId + ", username=" + username+ ", userage=" + Userage + "] ";}}
2. After the mapping class is created, the MyBatis mapping file (i.e. the mapper file) is created and is located in the Mapper package of Conf with SRC peers, with the following file contents:
<?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.mucfc.mapper.UserMapper" ><!-- query single record - <select id= " Selectuserbyid "parametertype=" int "resulttype=" User "> select * from t_user where userId = #{userid} </ Select> </mapper>
3, create the map Usermapper class as below, in the package Com.mucfc.mapper
Package Com.mucfc.mapper;import com.mucfc.model.user;/** * Mapper Mapping class * @author Linbingwen * @time 2015.5.15 */public Inter Face Usermapper {public User selectuserbyid (int userId);}
4. Creating the DAO layer of operational Data
Package com.mucfc.dao;import com.mucfc.model.user;/** * DAO interface Layer * @author Linbingwen * @time 2015.5.15 */public Interface U Serdao {/** * Query user information based on user ID * @param ID * @return */public user Finduserbyid (int id);}
then the corresponding implementation layer
Package Com.mucfc.dao;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.component;import Com.mucfc.mapper.usermapper;import com.mucfc.model.User;/** * DAO Implementation Layer * @author Linbingwen * @time 2015.5.15 */@Componentpublic class Userdaoimpl implements userdao{ @Autowiredpri Vate usermapper usermapper; @Overridepublic user Finduserbyid (int id) {User user = Usermapper.selectuserbyid (ID); return u Ser }}
5. Configure the MyBatis configuration file in conf:
MYBATISCONF.XMLL in conf, note the path.
<?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> <!--Configure the alias of the mapping class--<typeAliases> <typealias alias= "User" type= " Com.mucfc.model.User "/> </typeAliases> <!--Configuring the path to the mapper file-- <mappers> <mapper resource= "Mapper/usermapper.xml"/> </mappers></configuration>
If you do not integrate with spring, this configuration file also needs to configure the data source information, after the integration with spring data source is configured in the spring configuration file, only need to configure the path of the mapping file.
Third, configure Spring
1. Configure the spring configuration file in Web-inf:
Springconf.xml in Web-inf, note the path.
<?xml version= "1.0" encoding= "UTF-8"? ><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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= "Http://www.springframework.org/schem A/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframewor K.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd Http://www.springframe Work.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "><!--configuration Data source --><bean id= "DataSource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >< Property Name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/><property name= "url" value= "jdbc:mysql:// Localhost:3306/test "/><property name=" username "value=" root "/>&lT;property name= "Password" value= "[email protected]"/></bean><bean id= "sqlsessionfactory" class= " Org.mybatis.spring.SqlSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/><!--< Property Name= "Mapperlocations" value= "Conf/mapper/usermapper.xml"/>--><property name= "ConfigLocation" Value= "Conf/mybatisconf.xml"/><!--<property name= "Typealiasespackage" value= " Com.tiantian.ckeditor.model "/>--></bean> <bean id=" Usermapper "class=" Org.mybatis.spring.mapper.MapperFactoryBean "> <property name=" mapperinterface "value=" COM.MUCFC. Mapper. Usermapper "/> <property name=" sqlsessionfactory "ref=" Sqlsessionfactory "/> </bean> <!--from The bean--><context:component-scan base-package= "Com.mucfc.dao" of the scanned annotations/></beans>
2. Start spring in Web. XML
Web. XML put in Web-inf
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><!--Configure the initial open page--><welcome-file-list>< welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file>< welcome-file>index.jsp</welcome-file></welcome-file-list><!--Spring Container loading--><listener ><listener-class>org.springframework.web.context.contextloaderlistener</listener-class></ Listener><context-param><param-name>contextconfiglocation</param-name><param-value> /web-inf/springconf.xml</param-value></context-param></web-app>
3, read the bean, to find
Set the following in index.jsp:
Index.jsp placed in WebContent
<%@ page import= "Com.mucfc.dao.UserDao"%><% @page import= " Org.springframework.web.context.WebApplicationContext "%><% @pageimport =" Org.springframework.web.context.support.WebApplicationContextUtils "%><%@ page language=" java "contenttype=" Text/html "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Four, the Operation1. Run on server
2. Browser input: http://localhost:8080/MybatisLearningChapter5/
The results are as follows:
Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka
This article works free download
MyBatis and Spring Integration Create Web project