Spring4 SRPINGMVC Mybatis Integrated source and process

Source: Internet
Author: User

Basic preparatory work

1, install JDK1.6 above version, installation and configuration

2. Download mybatis-3.2.0 version: https://repo1.maven.org/maven2/org/mybatis/mybatis/

3. Download mybatis-spring-1.2.1 version: https://repo1.maven.org/maven2/org/mybatis/mybatis-spring/

4, the Spring-4.0.0 version

5, tomacat6.x or above version can be

Of course, these jars are not enough, also need MySQL database and drive, log4j jar and so on. Let's start today's trip:

"Java Framework source code download"

First step: Create a database table

Under Navicat, execute the following SQL command to create the database MyBatis and table T_user

CREATE DATABASE IF not EXISTS mybatis;  Use MyBatis;  CREATE TABLE T_user  • (  •    user_id int (one) not null auto_increment,  •    user_name varchar () NOT NULL,  •    user_age varchar () NOT NULL,  •    PRIMARY KEY (user_id)  

Let's take a look at the full list of items before proceeding to the following content

Step two: Add a jar Package

For the contents of the following code, we will no longer one by one posted out, just the most important content posted out, we can download the source code.

Step three: Create the model

Create a model package and create a User.java file under it.

01.package Com.tgb.model;  03./** 04. * User. * @author Liang *. * *. */  08.public class User {.  Ten.    private int id;  One.    private String age;  A.    private String userName;  Public    User () {  .        Super ();  .    public int getId () {  .        return ID;  .    }  .    public void setId (int id) {  .        This.id = ID;  .    }.    Public String Getage () {  .        return age;  .    public void Setage (String age) {  .        This.age = age;  .    Public String GetUserName () {  .        return userName;  .    }    public void Setusername (String userName) {  .        This.username = UserName;  .    public User (int ID, string age, String userName) {  .        Super ();        this.id = ID;  PNs.        this.age = age;        this.username = userName;  40.}  

Fourth step: Creating the DAO Interface

Create a package mapper, and under it create a Usermapper.java file as the DAO interface.

01.package Com.tgb.mapper;  03.import java.util.List;  05.import Com.tgb.model.User;  .  07.public Interface Usermapper {  Geneva.  .    void Save (user user);  .    Boolean update (user user);  One.    Boolean delete (int id);    User FindByID (int id);    list<user> findAll ();  14.}

Fifth step: Implementing the DAO Interface

Create a Usermapper.xml file under the DAO package as an implementation of the DAO interface created in the previous step.

01.<?xml version= "1.0" encoding= "UTF-8"?> 02.<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" 03. "    Http://mybatis.org/dtd/mybatis-3-mapper.dtd "> 04.<!--05.    Namespace: must be consistent with the full class name of the corresponding interface 06.      ID: must match a corresponding method name of the corresponding interface 07.      --09.<mapper namespace= "Com.tgb.mapper.UserMapper" > 10.        <insert id= "Save" parametertype= "User" > 12.    Insert into T_user (user_name,user_age) VALUES (#{username},#{age}) 13.      </insert> 14.        <update id= "Update" parametertype= "User" > 16.    Update T_user set User_name=#{username},user_age=#{age} where User_id=#{id} 17.      </update> 18.        <delete id= "Delete" parametertype= "int" > 20.    Delete from T_user where User_id=#{id} 21.      </delete> 22.    <!--the alias class name configured in the Mybsits_config, or you can directly configure Resulttype to class road strength and 24.        <select id= "FindByID" parametertype= "int" resulttype= "User" > 25. Select user_id Id,user_naMe username,user_age age from T_user where User_id=#{id} 26.      </select> 27.        <select id= "FindAll" resulttype= "User" > 29.    Select user_id id,user_name username,user_age age from T_user 30.      </select> 31. 32.</mapper>

Here are a few notes on this XML file: 1, namespace must be the same as the corresponding interface full class name. 2, the ID must match the corresponding interface of a corresponding method name must be the same as the method in the Usermapper.java interface.

Sixth step: Integration of MyBatis and spring

The integration of MyBatis and spring is the focus of this blog post, and the content that needs to be configured is explained in more detail below.

01.<?xml version= "1.0" encoding= "UTF-8"?> 02.<beans xmlns= "Http://www.springframework.org/schema/beans"    Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" 04.    xmlns:context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" 05.        xsi:schemalocation= "06.        Http://www.springframework.org/schema/beans 07.        Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 08.        Http://www.springframework.org/schema/context 09.        Http://www.springframework.org/schema/context/spring-context-4.0.xsd 10.        Http://www.springframework.org/schema/tx 11.  Http://www.springframework.org/schema/tx/spring-tx-4.0.xsd "> 12. <!--1.    Data Source: Drivermanagerdatasource-14.        <bean id= "DataSource" 15.        class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > 16. <property name= "DriverclassnaMe "value=" Com.mysql.jdbc.Driver "/> 17.        <property name= "url" value= "Jdbc:mysql://localhost:3306/mybatis"/> 18.        <property name= "username" value= "root"/> 19.    <property name= "Password" value= "123456"/> 20.  </bean> 21.        <!--23.  2. MyBatis's sqlsession factory: Sqlsessionfactorybean dataSource: Reference data source 24.    MyBatis defines the data source and agrees to load configuration 26.    -27.        <bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > 28.        <property name= "DataSource" ref= "DataSource" ></property> 29.    <property name= "configlocation" value= "Classpath:config/mybatis-config.xml"/> 30.  </bean> 31.        <!--33.  3. MyBatis automatic Scan load SQL map File/interface: Mapperscannerconfigurer sqlsessionfactory 34.    Basepackage: Specifies the package that contains the SQL mapping File/interface (auto-scan) 36.    -37.        <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > 38. <property name= "BasepAckage "value=" Com.tgb.mapper "></property> 39.    <property name= "Sqlsessionfactory" ref= "Sqlsessionfactory" ></property> 40.  </bean> 41.        <!--43.    4. Transaction management: Datasourcetransactionmanager DataSource: Reference the data source defined above 44.    -45.        <bean id= "Txmanager" 46.        class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > 47.    <property name= "DataSource" ref= "DataSource" ></property> 48.  </bean> 49. <!--5.         Use declarative transaction 51.     Transaction-manager: References the transaction manager 52 defined above.    -53.  <tx:annotation-driven transaction-manager= "Txmanager"/> 54.   55.</beans>

Seventh Step: MyBatis's configuration file

01.<?xml version= "1.0" encoding= "UTF-8"?> 02.<!  DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en"   03. " Http://mybatis.org/dtd/mybatis-3-config.dtd ">  04.<configuration>  .  .    <!--entity class, abbreviation-set alias---  .    <typeAliases>  .        <typealias alias= "User" type= "Com.tgb.model.User"/>  .    </typeAliases>  .    <!--Entity Interface Mapping Resources  --one.    <!--.        Note: If the Xxmapper.xml configuration file is placed in and Xxmapper.java the Unified directory, Mappers can also be omitted, because Org.mybatis.spring.mapper.MapperFactoryBean default will be to find and Xxmapper.java Xxmapper.xml of the same directory and name.  --.    <mappers>  .        <mapper resource= "Com/tgb/mapper/usermapper.xml"/>  .    </mappers>  .  18.</configuration>  

Java enterprise-Class generic rights security framework source SPRINGMVC MyBatis or Hibernate+ehcache Shiro Druid Bootstrap HTML5

"Java Framework source code download"

Spring4 SRPINGMVC Mybatis Integrated source and process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.