Basic use of Mybatis

1. Use eclipse to create a java project, jdk uses 1.7.0_72

2. Guide package: add mybatis core package, dependency package, and data-driven package.

3. Log: create log4j.properties

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

4. Create SqlMapConfig.xml (main configuration file)

PUBLIC "-//mybatis.org//DTDConfig 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

   <!-- The environments configuration will be abolished after integration with spring -->

   <environments default="development">

      <environment id="development">

      <!-- Use jdbc transaction management -->

         <transactionManager type="JDBC"/>

      <!-- Database connection pool -->

         <dataSource type="POOLED">

            <property name="driver"value="com.mysql.jdbc.Driver" />

            <property name="url"value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/>

            <property name="username"value="root" />

            <property name="password"value="root" />

         </dataSource>

      </environment>

   </environments>

</configuration>

5. (Entity class) Pojo class is used as mybatis for sql mapping, po class usually corresponds to database table, User.java

6. Create UserMapper.xml in config

<?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">
<!-- namespace sql isolation-->
<mapper namespace="cn.itcast.mybatis.mapper.UserMapper">
<!--statementId parameterType The parameter type passed; resultType: Type of return value -->
   <select id="findById" parameterType="java.lang.Integer" resultType="uSEr">
   <!-- Placeholder: #{} 
       After the compilation is completed, if it is a string, it will Automatically add single quotes Zhang-----'Zhang'-->
   <!--Connector: ${}
       Rule: If the parameter is a basic data type, value must be written in braces, if it is pojo or map, braces The attribute name or key name must be written in -->
    select * from user where id = #{id} 
   </select>
   <insert id="insertUser" parameterType="cn.itcast.mybatis.pojo.User">
   <!--主键先生成,添加后就会有ID,Mysql的函数    -->
     <selectKey resultType="int" keyProperty="id"  order="AFTER">
select LAST_INSERT_ID()
     </selectKey>
      insert into user (username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address});
   </insert>
</mapper>

7. Load the configuration file

The mybatis framework needs to load the mapping file and add Users.xml to SqlMapConfig.xml as follows:

<mappers>
<!--  <mapper resource="user.xml"/> --> Load directly
<!--  <mapper resource="User2Mapper.xml"/> --> Load directly
<!--  <mapper class ="cn.itcast.mybatis.mapper.UserMapper"/> --> The Dao interface is written
 <package name="cn.itcast.mybatis.mapper"/> package name

</mappers>

*** Mybatis is developed as a dao (no need to write the entity class dynamic proxy to help us create it)
    Rules: 1. The full path of the interface is consistent with the namespace in the mapping file
          2. The method name in the interface is consistent with the statementId in the mapping file
 3 , The parameter type and return value type of the interface method should be consistent with the parameterType and resultType in the mapping file.
 4. The interface name and the name of the mapping file should be consistent.
 5. The interface and the mapping file should be placed in the same directory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324398848&siteId=291194637