Creating mybatis program in mybatis introductory Series 1

Keywords: Java xml Mybatis Database SQL

Mybatis Basic Series I

Create the first mybatis program

Need to configure items

1. Configuration of two tag database connections and mapper in conf.xml, loading information of XML file

    <-- Configuration of database environment parameters    
        default There is a database environment that needs to be adopted, because there may be development,test Various environments  
    -->
    &lt;environments default="development"&gt;
        &lt;environment id="development"&gt;
            &lt;transactionManager type="JDBC"/&gt;
            &lt;datasource type="POOLED"&gt;
                &lt;property name="driver", value=""/>
                &lt;property name="url", value=""/>
                &lt;property name="user" value=""/>
                &lt;property name="password" value=""/>
            &lt;/datasource>
        &lt;/environment>

        <-- To configure mapper.xml Mapping file-->
        &lt;mappers>
            &lt;mapper resource=""></mapper>
        &lt;/mappers>
    &lt;/environments>

2. Writing mapper.xml file

&lt;?xml version="1.0" encoding="UTF-8" ?>

&lt;!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 

&lt;mapper namespace="org.lanqiao.entity.personMapper">

    &lt;select id="queryPersonById" resultType="org.lanqiao.entity.Person"  parameterType="int">
        select * from person where  id = #{id} 
    &lt;/select>
&lt;/mapper>

mybatis specifies that an SQL statement can have at most one input parameter

3. Write the corresponding classes in the database

4. Write test classes (optional)

public static void main(String[] args) throws IOException {
        //Load the MyBatis configuration file (to access the database)
        Reader reader = Resources.getResourceAsReader("conf.xml") ;
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ;
        //session - connection  
        SqlSession session = sessionFactory.openSession() ;
        //State is used to locate the SQL statement. It is made up of namespace.id in XXXmapper.xml.
        String statement = "org.lanqiao.entity.personMapper.queryPersonById" ;
        //When there are parameters, follow the state as the second parameter, and if there are no parameters, call the select function with only one parameter. 
        Student person = session.selectOne( statement,1 ) ;
        System.out.println(person);
        session.close(); 
        
    }

mybatis program upgrade

Writing a statement to locate SQL at a time is very troublesome. We can use dynamic proxy or interface development.

Differences from the first approach

Using the idea that convention is better than configuration, we omit statement and locate the SQL statement directly according to convention.

step

Con. xml, XXXmapper.xml and entity classes are all written in the same way as above.
The difference is to add an interface of XXXmapper.java.
Requirement:
1. The class name of mapper.java should be the same as the namespace in mapper.xml
2. The method name in mapper.java is the same as the id corresponding to each SQL statement in mapper.xml
3. The input parameter type of the method in mapper.java is the same as @parameterType in mapper.xml (without @parameterType, it is a parameterless function).
4. The return value type of the method in mapper.java is the same as @resultType in mapper.xml.

Then the test function can be modified to

StudentMapper studentMapper = session.getMapper(StudentMapper.class) ;  
studentMapper.Method();

Program optimization

Optimize 1: Put the database connection information in a configuration file db.properties separately

In order to view and modify the information about database connection, we can choose to put it in A. properties configuration file separately and express it by key-value pair.
Then, after introducing the. properties file into the conf.xml file, the variables and values in the conf.xml file are referenced in the form of ${}.

Example:
db.properties file:

driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username=scott
password=tiger

conf.xml file:

&lt;properties  resource="db.properties"/>
&lt;dataSource type="POOLED">
        &lt;property name="driver" value="${driver}"/>
        &lt;property name="url" value="${url}"/>
        &lt;property name="username" value="${username}"/>
        &lt;property name="password" value="${password}"/>
&lt;dataSource/>

Optimize 2: Set aliases

Write @resultType in the mapper.xml file if it's an object type.
For example, resultType="org.lanqiao.entity.Person"

By setting aliases, we can simplify this way of writing.

How to set aliases

In the conf.xml file:

&lt;typeAliases>
    <- Set aliases individually - >
    &lt;typeAlias type="org.lanqiao.entity.Person" alias="Person">

    < -- Setting aliases in batches means that the aliases of all classes under this package are the class names after removing the package names-->
    &lt;package name="org.lanqiao.entity">
&lt;typeAliases>

Posted by NiteCloak on Mon, 29 Apr 2019 03:40:37 -0700