MyBatis framework of SSM [SSM]: acquaintance MyBatis

MyBatis framework Introduction

MyBatis persistence framework is to support outstanding ordinary SQL queries, stored procedures and advanced mappings. MyBatis eliminates almost all of the retrieved manually set JDBC code and parameters and the result set. MyBatis using simple XML or annotations for configuration and original mapping, the mapping interface classes to the entity and records in the database.
To interact with a database, the usual practice is to write SQL statements in Java code, Java code SQL statements, and coupled together is not conducive to post-maintenance modification, and MyBatis can help us separate SQL statements and Java code, to facilitate late due changes in demand and to modify the SQL statement.

Use MyBatis framework

1, into the desired package MyBatis jar:

This includes the core package MyBatis, Mysql connected driver package.
Here Insert Picture Description

2, create a test table:

Table data in the database as follows:
Here Insert Picture Description

3, create packages and interfaces in src:

Here package called "cn.jingpengchong.flower.dao", interface IFlowerDao:

package cn.jingpengchong.flower.dao;

public interface IFlowerDao {
	int count();
}
3, create xml global configuration files in src:

Here it is called "mybatis.xml" may also be a different name.

<?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>
	<!-- default:默认引用environment的id,当前所使用的环境 -->
	<environments default="default">
		<!-- 声明可以使用的环境 -->
		<environment id="default">
			<!-- 使用原生JDBC事务 -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- 使用数据库连接池 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- 这里指定映射的xml文件 -->
	</mappers>
</configuration>

<TransactionManager /> type attribute values ​​may be:

  • JDBC: JDBC transaction management using native affairs management
  • MANAGED: The Transaction Manager transferred to another container, native JDBC transactions setAutoMapping (false)

<DataSouce /> type attribute values ​​may be:

  • POOLED: use the database connection pool
  • UNPOOLED: not practical database connection pool, and direct use JDBC as
  • JNDI: java Naming Directory Interface Technology
4, create xml mapping file corresponding to the module:

Create a xml file named "flower.xml" under src.

<?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.jingpengchong.pojo.Flower">
	<!-- 
		id:方法名
		parameterType:参数类型
		resultType:返回值类型
	 -->
	 <!-- 查询数据总条数的方法 -->
	 <select id="count" resultType="int">
	 	select count(*) from flower
	 </select>
</mapper>

As for the namespace that uniquely identifies the file in the project, and indicates its associated interfaces.

5, the mapping file "flower.xml" added to "mybatis.xml" file <mappers> tag, as follows:
<mappers>
	<mapper resource="flower.xml"/>
</mappers>
6, the test (the latter with a framework to achieve):

Here a new package "cn.jingpengchong.test", in which a new file "Test.java":
. After the new SqlSessionFactoryBuilder () build (is) performed, mybatis mybatis.xml will first read the corresponding file InputStream, then in accordance with mybatis.xml mappers tag file found user_info.xml file, and then find the corresponding IUserInfoDao Dao layer interface according user_info.xml file, and generates a proxy object.

package cn.jingpengchong.test;

import java.io.IOException;
import java.io.InputStream;

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 cn.jingpengchong.flower.dao.IFlowerDao;

public class Test {
	public static void main(String[] args) throws IOException {
		//加载配置文件
		InputStream is = Resources.getResourceAsStream("mybatis.xml");
		//获取SqlSessionFactory对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		//获取SqlSession对象,代表与数据库的一次会话,用完需要关闭。注意:由于SqlSession为非线程安全的,所以该变量应定义为局部变量,不要定义成全局变量
		SqlSession session = factory.openSession();	
		//获取IFlowerDao接口实现类对象
		IFlowerDao mapper = session.getMapper(IFlowerDao.class);
		//调用count方法获取表中记录的总数
		int count = mapper.count();
		System.out.println(count);
		session.close();
	}
}

Run results: 5
attached: Mybatis the DTD file Download:

Published 128 original articles · won praise 17 · views 2739

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104031083