MyBatis first program: HelloWorld

Keywords: Java Mybatis Database xml JDBC

1. Download the jar package required by MyBatis

  • Download links of Github versions of MyBatis: https://github.com/mybatis/mybatis-3/releases
  • Download the JDBC driven jar package of the corresponding database

    

2. Prepare the database to use

create database School;
use School;
create table Users
(
  id int primary key,
  name varchar(200)
);

 

 

3. MyEclipse new WebProject project

Import the jar package of MyBatis to the WEB-INF/lib directory

  

4. Write the imported database.properties database configuration file

Create a new: database.properties file under src

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/school
username=root
password=root

 

5. Write a global control file for MyBatis

Create a new MyBatis-config.xml configuration file under src

<?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>
	<!-- Import database profile -->
	<properties resource="database.properties"></properties>
	<typeAliases>
		<package name="th.bean"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<!-- Processing mode JDBC -->
			<transactionManager type="JDBC"></transactionManager>
			<!-- Write data source -->
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- Configure mapped mapping -->
	<mappers>
		<mapper resource="th/dao/mapping/UserMapper.xml"></mapper>
	</mappers>
</configuration>

6. Writing Mapping mapping files (JavaBean s and database Mapping files)

New package: th.dao.mapping, new file: UserMapping.xml

<?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="th.dao.mapping.UserMapper" >
        //Query specified user based on ID
	<select id="getUser" parameterType="Integer" resultType="th.bean.User">
		select * from users where id = #{id}
	</select>
</mapper>

7. Right click builderpath - > addlibrary to add Junit from the project

     

8. Write entity class User

New package th.bean new class: User

package th.bean;

public class User {
	private int id;
	private String name;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 

9. Write Junit test class, create th.test and UserTest under src

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import th.bean.User;

public class UserTest {

	@org.junit.Test
	public void test{
		String resource = "MyBatis-config.xml";
		try {
			InputStream ins = Resources.getResourceAsStream(resource);
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(ins);
			SqlSession session = sessionFactory.openSession();
			User user = session.selectOne("th.dao.mapping.UserMapper.getUser",2);
			System.out.println("user id: "+user.getId()+"\t User name:"+user.getName());
     }    

Posted by tranzparency on Thu, 02 Apr 2020 14:05:48 -0700