Deep - The first program MyBatis

  He did not talk much, start our first Mybatis program immediately;

  The first program, of course, a reference document to carry out his official website MyBatis following address: https://mybatis.org/mybatis-3/zh/getting-started.html ;

  First, a source code is hosted GitHub, the following address: https://github.com/gdoujkzz/MybatisDemo  of FirstDemo;

  My first program MyBatis

  The first step: create Maven project, introduced Mybatis;

  Step Two: Create a table and insert data in the database, the script is as follows:

create table Test_User(
    Id number(10) primary key,
    Name varchar2(50) not null
)

insert into Test_User(id,name) values(1,'张三');
insert into Test_User(id,name) values(2,'李四');
insert into Test_User(id,name) values(3,'王五');
insert into Test_User(id,name) values(4,'赵六');

  Step 3: Create database and corresponding POJO class;

public class User implements Serializable {

    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("User{");
        sb.append("id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

  The fourth step to write global profile

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@//www.test.com:1601/mestst9"/>
                <property name="username" value="mespro"/>
                <property name="password" value="mesprotst123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/TestUserMapper.xml"></mapper>
    </mappers>
</configuration>

  The fifth step to prepare the mapping file

<?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="mapper/TestUserMapper">

    <select id="selectUser" resultType="com.gdou.mes.User">
        select * from test_user
    </select>

    <select id="selectUserById" resultType="com.gdou.mes.User">
        select * from test_user where id=#{id}
    </select>

</mapper>

  The sixth step of loading a configuration file, and begin operation

public class UserMapperTest {

    private SqlSessionFactory factory;

    @Before
    public void SetUp() throws Exception {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * Rigorous Test :-)
     */
    @Test
    public void selectTest() throws Exception {
        SqlSession session = factory.openSession();
        try {
            // query all data
            System.out.println ( " - ------------ query all the data start -------------------"); 
            List < the User > List = session.selectList ( "Mapper / TestUserMapper.selectUser");
             for ( the User  User : List) {
                System.out.println(user);
            }
            System.out.println ( " - ------------ ------------------- data query to end all"); 
            // Query single data
            System.out.println ( " - ------------- single query data start -------------------"); 
            the User  the User  = session.selectOne ( "Mapper / TestUserMapper.selectUserById", . 1 );
            System.out.println(user);
            System.out.println ( " - ------------- single data query end -------------------"); 
        } the catch (Exception ex) {
            System.out.println(ex);
        } finally {
            session.close();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/gdouzz/p/12040896.html