Detailed configuration instructions for mybatis-config

Source: Internet
Author: User
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// config 3.0 //" "> <configuration> <! -- The property configuration element can embody the configuration value in an attribute file and use the key of the configuration file as the placeholder application. propertiesjdbc. driverclassname = com. mySQL. JDBC. driverjdbc. url = JDBC: mysql: // localhost: 3306/mybatisdemojdbc. username = rootjdbc. password = admin can use application. the placeholder defined in the properties file can also be directly used to set the value --> <properties resource = "application. properties "> <property name =" username "value =" db_user "/> <property name =" password "value =" ver Ysecurepwd "/> </Properties> <! -- Global settings --> <Settings> <setting name = "cacheenabled" value = "true"/> <setting name = "lazyloadingenabled" value = "true"/> <setting name = "multipleresultsetsenabled" value = "true"/> <setting name = "usecolumnlabel" value = "true"/> <setting name = "usegeneratedkeys" value = "false"/> <setting name = "automappingbehavior" value = "partial"/> <setting name = "defaultexecutortype" value = "simple"/> <setting name = "DEFA Ultstatementtimeout "value =" 25000 "/> <setting name =" saferowboundsenabled "value =" false "/> <setting name =" mapunderscoretocamelcase "value =" false "/> <setting name = "localcachting" value = "session"/> <setting name = "jdbctypefornull" value = "other"/> <setting name = "lazyloadtriggermethods" value = "equals, clone, hashcode, tostring "/> </Settings> <! -- Type alias is mainly used to simplify the fully qualified class names for paramertype and resulttype In the Mapper file --> <typealiases> <typealias alias = "tutor" type = "com. mybatis3.domain. tutor "/> <! -- You can provide the package of the class that requires the alias. mybatis will automatically scan the an in the package and define a lowercase letter for each JavaBean. For example, this package has a student, so the alias is: Student. In addition: @ alias ("studentalias") public class student {...} the @ alias annotation overwrites the <typealiases> definition --> <package name = "com. mybatis3.domain "/> </typealiases> <! -- Type handle, type processor when mybatis executes an insert operation, it creates a preparedstatement object and executes a series of operations (detailed below). In this process, there is a setxxx () in the process of setting values for placeholders, xxx can be any type of int, String, date. So what is the basis for mybatis to determine whether setint () or setstring () is used? In fact, mybatis is determined by using type handlers. Mybatis uses built-in type processors for the following types: all basic data types, basic types of Package Types, byte [], Java. util. date, Java. SQL. date, Java, SQL. time, Java. SQL. timestamp, Java Enumeration type, etc. So when mybatis finds that the attribute type belongs to the above type, it will use the corresponding type processor to set the value to preparedstatement. Similarly, when constructing a JavaBean from the SQL result set, there are also similar processes. --> <Typehandlers> <typehandler handler = "com. mybatis3.typehandlers. phonetypehandler"/> <package name = "com. mybatis3.typehandlers"/> </typehandlers> <! -- Database connection environment configuration --> <environments default = "development"> <! -- Environment: mybatis can have multiple datascource environments, such as Dev (development) and test (test). You can set the desired environment ID by setting the environment value by default. If an application needs to connect to multiple databases, you need to set each database to a separate environment and set a sqlsessionfactory for each database --> <environment id = "development"> <! -- The Transaction Manager type1: JDBC: mybatis internally uses jdbctransactionfactory to create transectionmanager. For example, to deploy an application to Tomcat, you need the application to manage the program type2: managed (managed, the application itself does not manage practices, and is managed by the server ): mybatis internally uses managedtransactionfactory to create transactions and manage its transactionmanager. For example, when a javaee application is deployed on a JBoss, WebLogic, and glassfish application server, they use ejbs to manage transactions on the application server. In these management environments, you can use the managed Transaction Manager. --> <Transactionmanager type = "JDBC"/> <! -- Data Source cetcetype1: unpooled: Creates a new link for each database operation and closes it. Suitable for the case of small data concurrency type2: Pooled: will wear a database connection pool, the development and testing phase of the common mode type3: JNDI: obtain the database connection from the configured JNDI data source on the server, in the production environment, priority --> <datasource type = "pooled"> <property name = "driver" value = "$ {JDBC. driverclassname} "/> <property name =" url "value =" $ {JDBC. URL} "/> <property name =" username "value =" $ {JDBC. username} "/> <property name =" password "value =" $ {JDBC. password} "/> </datasource> </environment> <environment id =" production "> <tra Nsactionmanager type = "managed"/> <datasource type = "JNDI"> <property name = "data_source" value = "Java: COMP/jdbc/mybatisdemod "/> </datasource> </environment> </environments> <! -- Mapper file ing --> <mappers> <mapper resource = "com/mybatis3/mappers/studentmapper. XML "/> <mapper url =" file: // D:/mybatisdemo/mappers/tutormapper. XML "/> <mapper class =" com. mybatis3.mappers. tutormapper "/> </mappers> <! -- Appendix: 1. mybatis executes an insert statement. 1) create a placeholder preparedstatement interface, as shown below: Java code preparedstatement pstmt = connection. preparestatement ("insert into students (stud_id, name, email, DOB) values (?,?,?,?) "); 2) Check the type of the studid property of the student object, and then use the appropriate setxxx method to set the parameter value. Studid is of the integer type, so the setint () method is used: Java code pstmt. setint (1, student. getstudid (); 3) Similarly, for the name and email attributes, both the string type and mybatis use the setstring () method to set parameters. Java codepstmt. setstring (2, student. getname (); pstmt. setstring (3, student. getemail (); 4) As for the Dob attribute, mybatis uses the setdate () method to set the value of the placeholder position in Dob. 5) mybaits will convert Java. util. the date type is converted to into Java. SQL. timestamp and set the value to pstmt. settimestamp (4, new timestamp (student. getdob ()). gettime (); 2. Custom typehandler assume that the students table has a phone field of Type varchar (15), and the JavaBean student class has a phonenumber attribute of the phonenumber class definition type. Java code public class phonenumber {private string countrycode; private string statecode; private string number; Public phonenumber (){//...} public phonenumber (string countrycode, string statecode, string number) {This. countrycode = countrycode; this. statecode = statecode; this. number = number;} public phonenumber (string) {If (string! = NULL) {string [] parts = string. split ("-"); If (parts. length> 0) This. countrycode = parts [0]; If (parts. length> 1) This. statecode = parts [1]; If (parts. length> 2) This. number = parts [2] ;}} Public String getasstring () {return countrycode + "-" + statecode + "-" + number ;} // setters and getters} public class student {private integer ID; private string name; private string email; private phonenumber phone; // se Tters and getters} XML Code <insert id = "insertstudent" parametertype = "student"> insert into students (name, email, phone) values (# {name }, # {email },# {PHONE}) </Insert> here, the phone parameter must be passed to # {PHONE}, while the phone object is of the phonenumber type. However, mybatis does not know how to process this type of object. To let mybatis understand how to deal with this custom Java object type, such as phonenumber, we can create a custom type processor, as shown below: 1) mybatis provides the abstract class basetypehandler <t>. We can inherit this class to create a custom type processor. Java code packagecom. mybatis3.typehandlers; importjava. SQL. callablestatement; importjava. SQL. preparedstatement; importjava. SQL. resultset; importjava. SQL. sqlexception; importorg. apache. ibatis. type. basetypehandler; importorg. apache. ibatis. type. jdbctype; importcom. mybatis3.domain. phonenumber; public class phonetypehandler extends basetypehandler <phonenumber >{@ overridepublic void setnonnullparameter (preparedst Atement ps, int I, phonenumber parameter, jdbctype) throwssqlexception {ps. setstring (I, parameter. getasstring () ;}@ overridepublic phonenumber getnullableresult (resultset RS, string columnname) throws sqlexception {return New phonenumber (RS. getstring (columnname) ;}@ overridepublic phonenumber getnullableresult (resultset RS, int columnindex) throws sqlexception {return New phonenumber (RS. getstring (Columnindex) ;}@ overridepublic phonenumber getnullableresult (callablestatement CS, int columnindex) throws sqlexception {return New phonenumber (CS. getstring (columnindex) ;}} 2) We use ps. setstring () and Rs. the getstring () method is used because the phone column is of the varchar type. 3) once we implement a custom type processor, We need to register it in the mybatis-config.xml: XML Code <? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// config 3.0 // en" ""> <configuration> <properties resource = "application. properties "/> <typehandlers> <typehandler handler =" com. mybatis3.typehandlers. phonetypehandler "/> </typehandlers> </configuration> after registering phonetypehandler, mybatis can store the phone-type object values on varchar columns. --> </Configuration>

Reference: Java persistence with mybatis 3 (Chinese Version)

This article from the "unruly Wind" blog, please be sure to keep this source

Detailed configuration instructions for mybatis-config

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: 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.