Basic introduction to Mybatis (query case)

  1. Prepare a java project
  2. Import Mybatis jar package and dependent packages, as well as database driver package and connection pool jar package asm-3.3.1.jar cglib-2.2.2.jar
    commons-logging-1.1.1.jar javassist-3.17.1-GA .jar mybatis-3.2.1.jar
    mysql-connector-java-5.1.26-bin.jar
  3. Create a resource source folder to put the database connection resource file db.properties
    db.properties
driverName=com.mysql.jdbc.Driver
# 这里字符集时候固定写法,只能在url中
url=jdbc:mysql://localhost:3306/0407?CharactorEncoding=utf8
user=user
password=user
  1. Database import, according to the database to write entity classes
  2. Write Dao layer code
    Here we use querying an object as an example
	//IProductDao里面代码
	/**
	 * 查询一个商品
	 */
	Product get(Long id);
	////ProductDaoImpl里面代码
 @Override
	public Product get(Long id) {
		return null;
	}
  1. Create a resource source folder to put Mybatis main configuration file Confinguration, xml
<?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">
<!-- Mybatis主配置文件的根标签 -->
<configuration>
   <!-- 加载属性文件,这里不要写classpath,因为resource资源会自动加载classpath下的文件 -->
   <properties resource="db.properties"></properties>

   <!-- 配置别名  -->
   <typeAliases>
   	<!-- 这个包下面的类都可以写简写:要么直接写类名  ,要么写类名的首字母小写 -->
   	<package name="cn.itsource.domain"/>
   </typeAliases>
   
   <!-- 环境们:可以配置多个环境,默认用的是id为development的一个环境 -->
   <environments default="development">
   	<!-- 配置一个环境 -->
   	<environment id="development">
   		<!-- 事务管理器:用的是JDBC的事务 -->
   		<transactionManager type="JDBC" />
   		<!-- 数据源 -->
   		<dataSource type="POOLED">
   			<property name="driver" value="${driverClassName}" />
   			<property name="url" value="${url}" />
   			<property name="username" value="${username}" />
   			<property name="password" value="${password}" />
   		</dataSource>
   	</environment>
   </environments>
   <mappers>
   	<!-- <mapper resource="cn/itsource/domain/ProductMapper.xml" /> -->
   	<mapper resource="cn/itsource/mapper/ProductMapper.xml" />
   </mappers>
</configuration>

7. Create a new xml file in the entity class package [entity class class name Mapper.xml]

<!-- 
	mapper - 映射sql
	namespace - 一般是接口的完全限定名,相当于区分类的包名
 -->
<mapper namespace="cn.itsource.dao.IProductDao">
	<!-- 
		select - 查询  
		id - 是sql语句的唯一表示【namespace.id】,一般是接口中的方法名
		parameterType - 参数类型
		resultType - 返回值类型
	 -->
	<select id="loadOne" parameterType="long" 
		resultType="cn.itsource.domain.Product">
		select * from product where id = #{id}
	</select>
</mapper>

8. Build SqlSessionFactory object through SqlSessionFactoryBuilder
9. Use SqlSessionFactory object to get SqlSession object
10. Call SqlSession API to operate database

//8-10步在DaoImpl类里实现
try {
			String str="Configuration.xml";
			Reader r;
			//将配置文件读取为流 -- 将配置文件加载到内存中
			r=Resources.getResourceAsReader(str);
			//根据流区构建工厂
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(r);
			//根据工厂创建一个SQLSession对象
			SqlSession session = factory.openSession();
			//通过SQLSession对象区获取方法查询
			//第一个参数是sql标识  用namespace+方法名
			Product p = session.selectOne("it.show.dao.IProductDao.get", id);
			return p;
		} catch (IOException e) {
			e.printStackTrace();
		}

11. Test

		Product product = dao.get(10l);
		System.out.println(product);

12. Execution process: first load the main configuration file, load the mapper mapping file in the main configuration file, after loading these two files, you can call the API of the ORM framework to perform database operations

Published 23 original articles · praised 1 · visits 178

Guess you like

Origin blog.csdn.net/weixin_45528650/article/details/105367615