---restore content starts---
What is MyBatis?
MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually sets parameters and gets the result set.
MyBatis can use simple XML or annotations for configuration and native maps to map interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database.
Installation
To use MyBatis, simply place the Mybatis-x.x.x.jar file in Classpath.
If you use Maven to build your project, you need to place the following dependency code in the Pom.xml file:
<dependency> <groupId>org.mybatis</groupId> <artifactid>mybatis</artifactid > <version>x.x.x</version></dependency>
The core is a two configuration file:
A configuration file for config. mybatis
A mapping file for Xxxmapper.xml, which is the entity class
XML config file (configuration. XML) contains the core settings for the MyBatis system, including the data source (DataSource) that gets the database connection instance, and the transaction manager (TransactionManager) that determines the scope and control of the transaction. Example configuration file:
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Configuration3Public "-//mybatis.org//dtd Config 3.0//en"4"Http://mybatis.org/dtd/mybatis-3-config.dtd" >5<configuration>6<typeAliases>7<typealias alias= "User" type= "Com.jikexueyuancrm.entity.User"/>8</typeAliases>9<environmentsdefault= "Development" > Default environment ID is developmentTen<environment id= "Development" > Define ID for Environment element One<transactionmanager type= "jdbc"/> transaction manager configuration type is JDBC
jdbc– This configuration is the direct use of the JDBC commit and rollback settings, which depend on the connection from the data source to manage the transaction scope.
A<datasource type= "Pooled" > -<property name= "Driver" value= "Com.mysql.jdbc.Driver"/> -<property name= "url" value= "Jdbc:mysql://192.168.0.118:3306/jikexueyuancrm"/> the<property name= "username" value= "admin"/> -<property name= "Password" value= "666666"/> -</dataSource> -</environment> +</environments> -<mappers> +<mapper resource= "Com/mybatis3/mappers/usermapper.xml"/> A</mappers> at</configuration>
Entity class User:
public class User {
Private int ID; Private String username; Private String password; Public String toString () { return ' ID: ' +id+ ' username: ' +username + ' password "+password;
... Omit Get,set method }}
mapping Files Usermaper.xml
<?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.my Batis3.mappers.UserMapper "> <select id=" Getuserbyid "parametertype=" int "Resulttype= "User" >Select* FROM User where id=#{id}</select> <insert id= "AddUser" usegeneratedkeys= "true" keyproperty= "id"ParameterType= "User" >INSERT INTO User (Username,password) VALUES (#{username},#{password})</insert> <update id= "UpdateUser" >Update user set username=#{username}, password=#{password} where id=#{id}</update> <delete id= "deleteuser" parametertype= "int" >Delete from User where ID=#{id}</delete></mapper>
Output:
2016-07-30 22:53:02,762 [main] DEBUG [com.mybatis3.mappers.UserMapper.getUserById]-==> preparing:select * From User where id=? 2016-07-30 22:53:02,807 [main] DEBUG [com.mybatis3.mappers.UserMapper.getUserById]-==> parameters:2(Integer) 2016-07-30 22:53:02,838 [main] DEBUG [com.mybatis3.mappers.UserMapper.getUserById]-<== total:1 2016-07-30 22:53:02,840 [main] DEBUG [Com.mybatis3.mappers.UserMapper.updateUser]-==> Preparing:update user set username=?, password=? where id=? 2016-07-30 22:53:02,841 [main] DEBUG [Com.mybatis3.mappers.UserMapper.updateUser]-==> Parameters:wangwu (String), Dsfsfs (String), 1(Integer) 2016-07-30 22:53:02,879 [main] DEBUG [ Com.mybatis3.mappers.UserMapper.updateUser]-<== updates:1 2 username:hehe Password 123123
Select
The query statement is one of the most commonly used elements in MyBatis, and the value of light energy in the database is not very good, if it can be re-extracted to be useful, most applications are also more frequent queries than changes. For each insert, update, or delete operation, more than one query operation is usually appropriate. This is one of the basic principles of MyBatis and the reason for putting focus and effort into query and result mapping. The select element of a simple query is very simple. Like what:
<select id= "Selectperson" parametertype= "int" resulttype= "HashMap" > * FROM person WHERE id = #{ ID}</select>
This statement is called Selectperson, takes an int (or Integer) type parameter, and returns an object of type HashMap, where the key is the column name and the value is the corresponding value in the result row.
Note parameter symbol:#{id}
This tells MyBatis to create a preprocessing statement parameter, through JDBC, such a parameter in SQL will be a "?" To identify and be passed to a new preprocessing statement, like this:
// Similar JDBC Code, not MyBatis ... String Selectperson = "SELECT * from the person WHERE id=?" = conn.preparestatement (selectperson);p s.setint (1,id);
Of course, this requires a lot of separate JDBC code to extract the results and map them to the object instance, which is where MyBatis saves you time. We need to understand the parameters and result mappings in detail, and we'll look at the details below.
---restore content ends---
Mybatis Study Notes 1