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 "-// Config 3.0//EN" "">

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=""/>
				<property name="url" value="jdbc:mysql://localhost:3306/****"/>
				<property name="username" value="root"/>
				<property name="password" value="ok"/>
			</dataSource>     <!-- Connection pool -->

Recommended method 2: call the file

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


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

<!-- Database connection configuration -->
<properties resource=""/>
<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}"/>

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


>Step 4: configure the PersonMapper.xml mapping file

1. Configure mapper in mybatis-config.xml

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

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})
	<select id="getPersonList" resultType="com.kgc.mybatis.pojo.Person">
		select * from t_person
	<update id="updatePesron" parameterType="com.kgc.mybatis.pojo.Person">
		update t_person set pname = #{pname} , age = #{age} , address = #{address} where pid = #{pid}
	<delete id="deletePerson" parameterType="int">
		delete from t_person where pid = #{pid}
	<select id="getPersonByPid" parameterType="int" resultType="com.kgc.mybatis.pojo.Person">
		select * from t_person where pid = #{pid}

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;
	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
	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!");
	public void getPersonListTest(){

		List<Person> PersonList = session.selectList("com.kgc.mybatis.pojo.getPersonList3");
		for (Person person : PersonList) {
	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!");
	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!");
	public void getPersonByPidTest(){
//		Person person = new Person(2,null,0,null);
		Person person = session.selectOne("com.kgc.mybatis.pojo.getPersonByPid", 6);
	public void teardown(){
//		5. Commit or roll back things
//		6. Close session



Tags: Mybatis Session xml JDBC

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