Mybatis Java note of (a): simple entry

1. Create a new database keeper

create database keeper

2. Create a table t_student

USE keeper;
DROP
TABLE IF EXISTS `t_student`; CREATE TABLE `t_student` ( `student_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生ID', `student_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '学生姓名', `student_age` int(11) NULL DEFAULT NULL COMMENT '学生年龄', PRIMARY KEY (`student_id`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

3. Import student data

The USE Keeper;
 the INSERT  the INTO `t_student` the VALUES ( null , ' John Doe ' , 23 is );
 the INSERT  the INTO ` t_student` the VALUES ( null , ' John Doe ' , 24 );
 the INSERT  the INTO `t_student` the VALUES ( null , ' Wang Wu ' , 25 );
 the INSERT  the INTO `t_student` the VALUES ( null , ' Zhao six ', 26);
INSERT INTO `t_student` VALUES (null, '韩七', 27);

 

4. Use the eclipse New Java Project

5. Create a folder lib, import jar package, and Build Path

6. Student class in the new package com.mybatis.demo.pojo

package com.mybatis.demo.pojo;

public class Student {
    private int studentId;
    private String studentName;
    private int studentAge;
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public int getStudentAge() {
        return studentAge;
    }
    public void setStudentAge(int studentAge) {
        this.studentAge = studentAge;
    }
    
    
}

7. In the new package com.mybatis.demo.pojo Student.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.mybatis.demo.pojo">
    <select id="getAllStudent" resultType="Student">
        select 
            student_id studentId,
            student_name studentName,
            student_age studentAge 
        from 
            t_student
    </select>
</mapper>

8. mybatisConfig.xml new directory in the src

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 对类起别名 -->
    <typeAliases>
        <!-- 类的别名默认为类名 -->
        <package name="com.mybatis.demo.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <!-- 使用JDBC事务管理 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 定义数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/keeper?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置sql映射配置文件 -->
    <mappers>
        <mapper resource="com/mybatis/demo/pojo/Student.xml"/>
    </mappers>
</configuration>

9. A new class at MybatisTest package com.mybatis.demo.test

package com.mybatis.demo.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mybatis.demo.pojo.Student;

public class MybatisTest {

    public static void main(String[] args) throws IOException {
        //加载mybatis的配置文件
        InputStream = Resources.getResourceAsStream InputStream ( "mybatisConfig.xml" );
         // get a SqlSessionFactory 
        SqlSessionFactory SqlSessionFactory = new new the SqlSessionFactoryBuilder () Build (inputStream);.
         // create an object using a SqlSessionFactory SqlSession 
        SqlSession SQLSESSION = sqlSessionFactory.openSession ();
         / / query results into multiple use selectList method, if the result is a use selectOne method 
        List <student> students = sqlSession.selectList ( "getAllStudent" );
         // traverse the print student information 
        for (student student: students) { 
            System. out.println (student.getStudentId () + "\ t" +
                    student.getStudentName()+"\t"+student.getStudentAge());
        }
    }

}

10. Run MybatisTest class, view the results

11. Description

  (1) custom package name, just put in some categories mybatisConfig.xml name and sql mapping configuration file into their own on the package name structure, the namespace Student.xml changed their name to the package;

  (2) Fill in your database connection information;

  (3) Result field name Student.xml in consistency with the Student class property name can be changed as follows:

  Which, mybatisConfig.xml open hump naming map

<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<! DOCTYPE the Configuration 
the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" 
"http://mybatis.org/dtd/mybatis config.dtd--3 " > 
< Configuration > 
    < Settings > 
        <-! open camelCasing mapping rules -> 
        < Setting name =" mapUnderscoreToCamelCase " value =" to true " /> 
    </ Settings > 
    <-! class since alias -> 
    < typeAliases > 
        <-! alias class defaults to the class name ->
        <package name="com.mybatis.demo.pojo"/>
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <!-- 使用JDBC事务管理 -->
            <transactionManager type="JDBC"></transactionManager>
            <!-- 定义数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/keeper?characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置sql映射配置文件 -->
    <mappers>
        <mapper resource="com/mybatis/demo/pojo/Student.xml"/>
    </mappers>
</configuration>

Student.xml read:

<?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.mybatis.demo.pojo">
    <select id="getAllStudent" resultType="Student">
        select 
            student_id,
            student_name,
            student_age
        from 
            t_student
    </select>
</mapper>

Re-run MybatisTest class result is the same.

Guess you like

Origin www.cnblogs.com/antusheng/p/12565098.html
Recommended