Detailed ---- Dao layer MyBatis

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45348687/article/details/102777978

The more common way to access the data layer:

1. JDBC: relational database access methods java native

  1. Database need to get connected to close the connection when accessing large databases, frequent consumption performance of each operation switch link.
  2. Need to manually write sql statement, there is the cost of learning.
  3. The results of the query need to manually package bean.
  4. No caching mechanism.
  5. sql statement written dead yet, need to modify the sql in the program you must modify the source file

2. Hibernate: Based on the concept of object-oriented framework Dao layer design, the basic idea is to maintain the object mapping table, the data table in the operation target by the operation, which can reduce or even eliminate the use of sql
6. The relatively heavy, inefficient good
7. when it comes to more complex queries, Hibernate object mode operation with them is very difficult, if not impossible to achieve, can only operate with sql
8. underlying need for frequent splicing sql, sql generate a lot of redundancy

3. MyBatis: is a semi-automatic target - Dao layer framework table mappings, it can automatically encapsulates an object, but still need to write your own sql.

Detailed MyBatis:

1. MyBatis structure:

Here Insert Picture Description

2. Mybatis Getting Case:

  • a. Create a java project, in which you imported the relevant Development Kit
  • b. Import constraint file
  • c. to write the configuration file sqlMapConfig.xml, configuration data source
<?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="mysql">
		<environment id="mysql">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql:///mybatis_test"/>
				<property name="username" value="root"/>
				<property name="password" value="root"/>
			</dataSource>
		</environment>
	</environments>
</configuration>

  • d. create tables, create bean
create database mybatisdb;
use mybatisdb;
create table user (
id int primary key auto_increment,
name varchar(255),
	age int
);
insert into user values (null,'aaa',19),(null,'bbb',29),(null,'ccc',39),(null,'ddd',22),(null,'eee',33);
package test.domain;

public class User {
	private int id;
	private String name;
	private int age;
	
	public User() {
	}
	
	public User(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}

  • e. the preparation of the mapping file, writing table and bean sql mapping relationship
<?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">
<mapper namespace="test.mybatis.beans.UserMapper">
	<select id="queryAll" resultType="test.mybatis.beans.User">
		select * from user
	</select>
</mapper>
  • f. Set the configuration file to map file sqlMapConfig.xml
<!--声明映射文件-->
<mappers>
	<mapper resource="test/mybatis/beans/UserMapper.xml">
</mappers>
  • g. Test Test Class
public class MybatisTest{
	@Test
	public void test01() throws Excepetion{
		//1.读取MyBatis核心配置文件
		InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
		//2.根据配置文件创建sqlSessionFactory
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		//3.创建SqlSession
		SqlSession session = factory.openSession();
		//4.执行操作
		List<User> list = session.selectList("test.mybatis.beans.UserMapper.queryAll")
		//5.遍历结果
		System.out.println(Arrays.toString(list.toArray()))
	}
}

Guess you like

Origin blog.csdn.net/weixin_45348687/article/details/102777978