Configuring MyBatis with XML

>Step 1: two packages need to be forerunner before configuration

>MyBatis-3.2.2.jar MyBatis framework
>Mysql-connector-java-3.1.10-bin.jar JDBC Driver

>Step 2: configure the environment

Under the mybatis-config.xml file, configure the following as the original state of the XML file

<?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></configuration>

Next, configure it in < configuration > < configuration >

Method 1: configure directly

	<environments default="hello_mysql">
		<environment id="hello_mysql">
			<transactionManager type="jdbc"/>  <!-- //Manager -- >
			<dataSource type="pooled">
				<property name="driver" value="org.gjt.mm.mysql.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/****"/>
				<property name="username" value="root"/>
				<property name="password" value="ok"/>
			</dataSource>     <!-- Connection pool -->
		</environment>
	</environments>

Recommended method 2: call the db.properties file

1. The configuration information of db.properties is imported into the related jar package as follows

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/****
    user=root
    password=root

2. The configuration information of mybatis-config.xml is as follows

<!-- Database connection configuration -->
<properties resource="db.properties"/>
<environments default="mysql_development">
    <environment id="mysql_development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
             <property name="driver" value="${driver}"/>
             <property name="url" value="${url}"/>
             <property name="username" value="${user}"/>
             <property name="password" value="${password}"/>
         </dataSource>
    </environment>
</environments>

>Step 3: create the corresponding database table and pojo class

slightly

>Step 4: configure the PersonMapper.xml mapping file

1. Configure mapper in mybatis-config.xml

        <mappers>
		<mapper resource="com/kgc/mybatis/pojo/PersonMapper.xml"/>
	</mappers>

2. Configure the corresponding add, delete, modify, query and other queries in Mapper.xml

	<insert id="addPerson" parameterType="com.kgc.mybatis.pojo.Person" useGeneratedKeys="true" keyProperty="pid">
		insert into t_person values(null,#{pname},#{age},#{address})
	</insert>
	<select id="getPersonList" resultType="com.kgc.mybatis.pojo.Person">
		select * from t_person
	</select>
	<update id="updatePesron" parameterType="com.kgc.mybatis.pojo.Person">
		update t_person set pname = #{pname} , age = #{age} , address = #{address} where pid = #{pid}
	</update>
	<delete id="deletePerson" parameterType="int">
		delete from t_person where pid = #{pid}
	</delete>
	<select id="getPersonByPid" parameterType="int" resultType="com.kgc.mybatis.pojo.Person">
		select * from t_person where pid = #{pid}
	</select>

Now that the basic configuration of MyBatis is complete, let's test and demonstrate the calling method

Basic steps:

//1. Load sqlMapConfig file

//2. Parse sqlMapConfig.xml, create SqlSessionFactory object, and encapsulate it in this object

//3. Create session object through factory

//4. Insert to return the number of affected rows

//5. Commit or roll back things

//6. Close session
	SqlSession session = null;
	@Before
	public void setup() {
//		1. Load sqlMapConfig file
		InputStream is;
		try {
			is = Resources.getResourceAsStream("sqlMapConfig.xml");
//		2. Parse sqlMapConfig.xml, create SqlSessionFactory object, and encapsulate it in this object
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//		3. Create session object through factory
			session = factory.openSession();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void addPersonTest(){
		Person person = new Person("lisi", 15, "shenzheng");
		// 4. Insert to return the number of affected rows
		int count = session.insert("com.kgc.mybatis.pojo.addPerson1", person);
		System.out.println(count > 0 ? "insert success!" : "insert fail!");
	}
	
	@Test
	public void getPersonListTest(){

		List<Person> PersonList = session.selectList("com.kgc.mybatis.pojo.getPersonList3");
		for (Person person : PersonList) {
			System.out.println(person);
		}
	}
	
	@Test
	public void updatePersonTest(){
		Person person = new Person(12,"xiaomin1",21,"dalian");
		int count = session.update("com.kgc.mybatis.pojo.updatePerson", person);
		System.out.println(count > 0 ? "update success!" : "update fail!");
	}
	
	
	@Test
	public void deletePersonTest(){
//		Person person = new Person(2,null,0,null);
		int count = session.update("com.kgc.mybatis.pojo.deletePerson", 5);
		System.out.println(count > 0 ? "delete success!" : "delete fail!");
	}
	
	@Test
	public void getPersonByPidTest(){
//		Person person = new Person(2,null,0,null);
		Person person = session.selectOne("com.kgc.mybatis.pojo.getPersonByPid", 6);
		System.out.println(person);
	}
	
	@After
	public void teardown(){
//		5. Commit or roll back things
		session.commit();
//		6. Close session
		if(session!=null){
			session.close();
		}
	}
	

 

 

Tags: Mybatis Session xml JDBC

Posted on Fri, 10 Jan 2020 13:59:04 -0500 by ozconnect