Spring Consolidated MyBatis (Configure mapper proxy with scan package)

Source: Internet
Author: User

Spring Consolidated MyBatis (Configure mapper proxy with scan package)
Pojo is an entity class generated from a table that has the same property name as the field name and an alias for a different SQL statement query.
First, lead the jar package

Entity class

public class User {    private Integer id;    private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

The first step:
Write the MyBatis configuration file Sqlmapconfig.xml (although it is possible to write nothing except headers, but must have).

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration></configuration>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Step Two:
Writing Spring configuration Files Applicationcontext.xml

(db.properties:文件里面放的是连接数据库的相关信息。jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/t003?characterEncoding=utf-8jdbc.username=test01jdbc.password=) 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
<?xml version= "1.0" encoding= "UTF-8"?><Beansxmlns="Http://www.springframework.org/schema/beans"xmlns:context="Http://www.springframework.org/schema/context"xmlns:p="Http://www.springframework.org/schema/p"xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xmlns:tx="Http://www.springframework.org/schema/tx"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-4.0.xsd "><!--load configuration files--<Context:property-placeholderlocation="Classpath:db.properties"/><!--database connection pool--<BeanId="DataSource"class="Org.apache.commons.dbcp.BasicDataSource"Destroy-method="Close" ><PropertyName="Driverclassname"Value="${jdbc.driver}"/><PropertyName="url"Value="${jdbc.url}"/><PropertyName="Username"Value="${jdbc.username}"/><PropertyName="Password"Value="${jdbc.password}"/><PropertyName="Maxactive"Value="Ten"/><PropertyName="Maxidle"Value="5"/></Bean><!--sqlsessonfactory Configuration--<BeanId="Sqlsessionfactory"class="Org.mybatis.spring.SqlSessionFactoryBean" ><!--configuration database connection pool--<PropertyName="DataSource"Te ="DataSource" ></Property><!--load configuration files--<property name= "configLocation" Span class= "Hljs-attribute" >value= "Classpath:SqlMapConfig.xml" > </property> </ bean> <bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer "> <property name= "basepackage" value= "cn.test"/> </bean></beans                 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

The fourth step is to write the XML file corresponding to the interface Usermapper.xml

<?xml version= "1.0" encoding= "UTF-8"?><! DOCTYPE mapperpublic "-//mybatis.org//dtd Mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><!--namespace is a namespace, a function of the isolation of SQL statements, but also the important role #{} is a placeholder, equivalent to the JDBC "?" ParameterType: The parameter type of the query Resulttype: The data type of the query result, If give Pojo should give full path. -<!--mapper Agent development rules: 1, namespace must be the full-qualified interface Name 2, Statementid must and interface method name consistent 3, the interface method parameter type and parametertype to be consistent 4, The return value type of the interface method must be the same as Resulttype-<MapperNamespace="Cn.test.mybatis.po.UserMapper" ><!--aliases are case-sensitive<SelectId="Getuserbyid"Parametertype="Int"Resulttype="Cn.test.mybatis.po.User" > select * from ' User ' WHERE id=#{id};</Select><!--If the query results return a list, Resulttype is set to the data type of an element in list ${} string concatenation instruction--<SelectId="Getuserbyname"Parametertype="String"Resulttype="Cn.test.mybatis.po.User" > select * from ' User ' WHERE username like '%${value}% '</Select><!--beautiful dividing line just test the simplest top two methods--< when!--parameter is Pojo, the name in #{} is the property of Pojo--<!--<insert id= "Insertuser" parametertype= "Cn.test.mybatis.po.User" > Keyproperty: Primary key Attribute Pojo for Resulttype: The data type of the corresponding primary key order: either before or after the INSERT statement executes. If you use the UUID key, you should be the primary key and then insert the data, you should use before <selectkey keyproperty= "id" resulttype= "int" order= "after" > select Last _insert_id () </selectKey> INSERT into user (username,birthday,sex,address) VALUES (#{username}, #{birthday}, #{ Sex}, #{address}) </insert>-<!--<select id= "Getuserbyqueryvo" parametertype= "Queryvo" resulttype= "User" > select * from ' user ' WHERE id=#{ User.ID}; </select><!--Query the number of records in a user table--<!--<select id= "Getusercount" resulttype= "int" > SELECT COUNT (*) from ' user ' </select> <sql id= ' Find_ User_list_where "> <where> <if test=" Id!=null "> and Id=#{id} </if> <if test=" username! = null and U Sername! = "" > and username like '%${username}% ' </if> </where> </sql> <sql id= "User_field_list" > Id,username,birthday,sex,address </sql>--<!--<select id= "finduserlist" parametertype= "user" resulttype= "user" > select <include refid= "User_field_list"/> from user <include refid= "find_user_list_where"/> </select>-- > <!--dynamic SQL foreach Test--<!--<select id= " Finduserbyids "parametertype=" Queryvo "resulttype=" User "> select <include refid=" User_field_list "/> From ' User ' <where> and ID in (1,10,20,21,31) <foreach collection= "IDs" item= "id" open= "and ID in (" close= ")" separator= "," > #{id} </foreach> </where> </select>--></ MAPPER>     
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • *
    • +
    • ,
    • ,
    • +
    • $
    • "
    • "
    • "
    • "
    • "
    • " "
    • " "
    • "
    • "
    • "
    • "
    • "

Final Test

PublicClassmybatistest {Private ApplicationContext ApplicationContext;@BeforePublicvoidInit ()Throws Exception {//initialize Spring container applicationcontext = new Classpathxmlapplicationcontext ( "Classpath:applicationContext.xml");}  @Test public void testfinduserbyid () {Usermapper usermapper = Applicationcontext.getbean (UserMapper.class); list<user> User = usermapper.getuserbyname (for (User user2:user) {System.out.println (User2.getusername ());}}  @Test public void test () {Usermapper usermapper = Applicationcontext.getbean (Usermapper.class); User Userbyid = Usermapper.getuserbyid (1); System.out.println (Userbyid); }}

Spring Consolidated MyBatis (Configure Mapper Agent with scan package)

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.