MyBatis implementation

What is MyBatis

MyBatis iBatis from the Apache open source projects, from iBatis3.x officially changed its name to MyBatis. It is an excellent persistence framework.

Why MyBatis

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. (Contact role properties file)

How to use MyBatis

  1. The introduction of the jar package
    Here Insert Picture Description
  2. Create an interface, a modular development in a manner corresponding to the database table
public interface IUserInfoDao {

	//这里我们定义一个抽象方法,通过id字段来获取user_name字段
	String getUserName(String id);
}
  1. Next, create the configuration xml file
  • User_info first to configure the module xml file, we first have to copy the code into the blue write the following code
<?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="live.sunhao.userinfo.dao.IUserInfoDao">
	<!-- 这是一个select标签 对应着select语句 这里的id对应着接口中的方法名,resultType是返回值类型 #{id}对应着方法中传入的参数值-->
 	<select id="getUserName" resultType="java.lang.String">
  		select user_name from user_info where id=#{id}
 	</select>
</mapper>
  • Again xml file configuration of the entire project, as above, we first have to copy the code into the blue write the following code
<?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="test"><!-- 这里default决定默认使用哪个环境,如下有两种环境 一个用来测试(test) 一个是正式开发的环境(dev) -->
  		<environment id="test">
   			<transactionManager type="JDBC"></transactionManager>
   			<dataSource type="POOLED">
    				<property name="driver" value="com.mysql.jdbc.Driver" />
    				<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
    				<property name="username" value="root" />
    				<property name="password" value="root" />
   			</dataSource>
  		</environment>
  		
  		<environment id="dev">
   			<transactionManager type="JDBC"></transactionManager>
   			<dataSource type="POOLED">
    				<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
    				<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:keeper" />
    				<property name="username" value="root" />
    				<property name="password" value="root" />
  	 		</dataSource>
	  		</environment>
	</environments>
 
 	<!-- 这里将每个模块的xml文件 配置到整个项目的xml文件中,我这里暂时就一个模块 -->
 	<mappers>
  		<mapper resource="user_info.xml"/>
 	</mappers>
</configuration>
  1. Write a test class
public class Test {
	public static void main(String[] args) {
  		try {
   			InputStream inputStream = Resources.getResourceAsStream("mybatis_config.xml");
   			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
   			//SqlSession对象代指一次数据库连接
   			SqlSession sqlSession = sqlSessionFactory.openSession();
   			IUserInfoDao userInfoDao = sqlSession.getMapper(IUserInfoDao.class);
   			System.out.println(userInfoDao.getClass().getName());
   			String userName = userInfoDao.getUserName("3ddcf637-15a8-49d9-a378-b3fa2f2f9c65");
   			System.out.println(userName);
  		} catch (IOException e) {
   			e.printStackTrace();
  		}
 	}
 }

Finally, look at the console output:
Here Insert Picture Description
We can see here a JDK dynamic proxy, userInfoDao agent class object is automatically implements the interface we created, enabling to find the user_name by 36 id

Published 101 original articles · won praise 3 · Views 2240

Guess you like

Origin blog.csdn.net/S_Tian/article/details/104251587