.1 create maven project, adding a dependency in pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cong</groupId> <artifactId>mybatics_firstDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <!-- mybatis核心包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!-- mysql驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> <!-- junit测试包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies> </project>
2.mysql database-related actions
3 .. Creating com.cong.pojo.Account class in the java directory
package com.cong.pojo; public class Account { private int id; private String name; private float money; 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; } public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
4. Create mybatis-context.xml file in the resources directory
<? xml Version = "1.0" encoding = "UTF-8"?> <! DOCTYPE the Configuration the PUBLIC "- // mybatis.org//DTD Config 3.0 // EN" "http://mybatis.org/dtd/mybatis config.dtd--3 "> <the configuration> <-! environment configuration -> <typeAliases> <-! namespaces, so AccountMapper return value would not write com.cong.pojo.Account, just write Account it -> < Package name = "com.cong.pojo"> </ Package > </ typeAliases> <Environments default = "Development"> <Environment ID = "Development"> <transactionManager type="JDBC"/>type = the transactionManager "the JDBC" /> <-! database connection configuration, dynamically acquires the content file config.properties here -> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/cong" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <!-- mapping文件路径配置 --> <mappers> <mapper resource="mapper/AccountMapper.xml"/> </mappers> </configuration>
5. Create mapper directory in the resources directory, create accountMapper.xml file in the directory mapper
<? xml Version = "1.0" encoding = "UTF-8"?> <! DOCTYPE Mapper the PUBLIC "- // mybatis.org//DTD Mapper 3.0 // EN" "http://mybatis.org/dtd/mybatis mapper.dtd--3 "> <-! Mapper root node mapping, namespace specified interface Dao full class name mybatis dynamically creates a basis for this interface implementation class to implement this interface, and this class is to achieve a target Mapper -> <Mapper namespace = "com.cong.pojo.Account"> <SELECT ID = "the findAll" the resultType = "the Account"> SELECT * from Account </ SELECT> <SELECT ID = "the findById" the parameterType = "int" resultType = "Account"> select * from account where id = #{id} </select> </mapper>
6. Create AccountTest test class under test, java directory
import com.cong.pojo.Account; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Test; import java.io.IOException; import java.io.Reader; import java.util.List; public class AccountTest { @Test public void accountFindById(){ SqlSession session = getSqlSession(); //传入参数查询,返回结果 Account account=session.selectOne("findById",1); //输出结果 System.out.println(account.toString()); //关闭session session.close(); } @Test public void accountFindAll(){ SqlSession session = getSqlSession(); List<Account> list = session.selectList("findAll"); for (Account account : list) { System.out.println(account.toString()); } session.close(); } private SqlSession getSqlSession() { //Read file names defined String Resources = "mybatis-config.xml" ; // Create stream Reader reader = null ; the try { // read mybatis-config.xml file reader to the object reader = Resources.getResourceAsReader (Resources); } the catch (IOException E) { e.printStackTrace (); } // initialize mybatis, create an instance of the class SqlSessionFactory SqlSessionFactory sqlMapper = new new the SqlSessionFactoryBuilder () Build (Reader);. // create a session instance return sqlMapper.openSession (); } }
7.findAll unit test results
8. The full directory structure