Mybaties Framework QuickStart program under IDEA Tools

This article describes the tool mybatis under IDEA Quick Start program is divided into the following five steps

Add 1 dependencies 

2 Write the subject pojo

3 write mapping file

4 Write the core profile

5 Testing Framework

Details are as follows

After establishing Module

1 add dependencies in the following code is added pox.xml

<properties>
<maven.coppiler.source>1.9</maven.coppiler.source>
<maven.coppiler.target>1.9</maven.coppiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>

2 write pojo object is the table of entity classes

3. Write the following code 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="cn.lijun.Users">
<select id="findAll" resultType="domain.Users">
SELECT * FROM users;
</select>

</mapper>

     

4 write core configuration file code is as follows

  

<?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" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis9501?characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!--映射文件路径-->
<mappers>
<mapper resource="cn/lijun/UsersMapper.xml"></mapper>
</mappers>
</configuration>

FIG. 5 testing frame to establish the corresponding packet in the test

code show as below

/**
* @author lijun
* @date 2019/6/28 11:44
*/
public class TestCusom {
@Test
public void test(){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("SqlMapConfig.xml");
// 创建工厂对象
System.out.println(inputStream);
SqlSessionFactoryBuilder sqlSessionFactoryBuilder= new SqlSessionFactoryBuilder();
System.out.println(sqlSessionFactoryBuilder);
SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sessionFactory.openSession();
System.out.println(sqlSession);
List<Object> list = sqlSession.selectList("cn.lijun.Dao.Users.findAll");
for(Object o :list){
System.out.println(o);
}
}
}

 

Guess you like

Origin www.cnblogs.com/lijun6/p/11105273.html