Mybatis3.2.1 Example 4: MySQL session integration with spring without Dao

Source: Internet
Author: User

The Mapper method provided by mybatis is confusing: an interface class is provided, but the implementation class cannot be found. In fact, this interface mainly provides a list of method names and parameters. After the Mapper agent class extracts the information, this interface is useless and thus does not need to be implemented, it still uses sqlsession for operations. From the perspective of the sqlsession method, we can directly provide the method name and parameter information to omit this Mapper, especially when using Dao (there are also interfaces, the Dao interface is basically the same as that of mapper. (Note: The method provided by sqlsession only supports passing in one method name and one parameter, while the interface provided by mapper can provide multiple parameters. The use of parameters will be described later)

The following is an example of using sqlsession directly:

(1) User bean carrying data: COM/mybatis/demo4/user. Java

Package COM. mybatis. demo4; public class user {/** user ID */private intid;/** user name */privatestring name;/** user password */privatestring password; Public intgetid () {returnid;} public voidsetid (int id) {This. id = ID;} publicstring getname () {return name;} public voidsetname (string name) {This. name = Name;} publicstring GetPassword () {returnpassword;} public voidsetpassword (string password) {This. password = password ;}}

(2) Data Source properties file: COM/mybatis/demo4/MySQL. Properties

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/bookstoreusername=rootpassword=123456

(3) Spring configuration file: COM/mybatis/demo4/applicationcontext. xml

(In this case, the Mapper configuration is removed and replaced by the sqlsession configuration)

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" 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.xsd"> <Bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "value =" classpath: COM/mybatis/demo4/MySQL. properties" /> </Bean> <! -- Data source --> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "value =" $ {driver} "/> <property name =" url "value =" $ {URL} "/> <property name =" username "value =" $ {username} "/> <property name =" password "value =" $ {password} "/> </ bean> <! -- Create sqlsessionfactory. You must specify the data source. The property name must be datasource --> <bean id = "sqlsessionfactory" class = "org. mybatis. spring. sqlsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" configlocation "value =" classpath: COM/mybatis/demo4/mybatis_config.xml "/> </bean> <! -- Use the sqlsession format. You do not need to use mapper again. --> <! -- Create a ER Mapper. The value of the mapperinterface attribute must be an interface class. --> <! -- <Bean id = "usermapper" class = "org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. mybatis. demo4.usermapper "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> --> <! -- Use sqlsession format --> <bean id = "sqlsession" class = "org. mybatis. spring. sqlsessiontemplate "> <constructor-Arg ref =" sqlsessionfactory "/> </bean> <! -- Data Access Dao, use sqlsession in Dao to query data, and customize the setter method corresponding to the attribute name in Dao --> <bean id = "userdao" class = "com. mybatis. demo4.userdaoimpl "> <property name =" sqlsession "ref =" sqlsession "/> </bean> </beans>

(4) mybatis configuration file: COM/mybatis/demo4/mybatis_config.xml (after integration with spring, many attributes of this configuration file are unavailable, such as setting and environment, mainly uses typealiases and mappers)

<?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><typeAliases><typeAlias alias="User" type="com.mybatis.demo4.User" /></typeAliases><mappers><mapper resource="com/mybatis/demo4/UserMappers.xml" /></mappers></configuration>

(5) mapper configuration file: COM/mybatis/demo3/usermappers. xml

(Detailed SQL configuration is provided. The select/insert/update/delete statement IDs must be the same as the Mapper method names)

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <! -- The namespace must be configured when mapper is used, and the value is the Mapper class path. If sqlsession is used directly, it cannot be configured. --> <mapper namespace = "com. mybatis. demo4.usermapper "> <resultmap id =" usermap "type =" user "> <ID property =" ID "column =" ID "/> <result property =" name "column =" name "/> <result property =" password "column =" password "/> </resultmap> <select id =" findbyname "parametertype =" string "resultmap =" usermap "> select * from users where name =#{ name} </SELECT> </mapper>

(6) Dao interface: COM/mybatis/demo4/userdao. Java

package com.mybatis.demo4;public interface UserDao {public User getUserByName(String name);}

(7) DAO implementation class: COM/mybatis/demo2/userdaoimpl. Java

Package COM. mybatis. demo4; import Org. mybatis. spring. sqlsessiontemplate; public class userdaoimpl implements userdao {/*** custom sqlsession variables and related getter/setter, in applicationcontext. the attribute name */Public sqlsessiontemplate sqlsession of Dao can be flexibly configured in XML; @ overridepublic user getuserbyname (string name) {return getsqlsession (). selectone ("com. mybatis. demo4.usermapper. findbyname ", name);} public sqlsessiontemplate getsqlsession () {return sqlsession;} public void setsqlsession (sqlsessiontemplate sqlsession) {This. sqlsession = sqlsession ;}}

(8) test class: COM/mybatis/demo2/usermanagerprgm. Java

package com.mybatis.demo4;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class UserManagerPrgm {public static void main(String[] args) {String configLocation = "com/mybatis/demo4/applicationContext.xml";ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);UserDao dao = (UserDao)context.getBean("userDao");User user = dao.getUserByName("Joe");System.out.println(user);}}

Related Article

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.