1、配置文件
<?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>org.example</groupId> <artifactId>2022227</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <!--资源插件,处理src/main/java中的properties、xml--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
2、总体结构
3、User实体类
package com.zhangshaopeng.pojo; public class User { private int id; private String name; private int qua; public User(int id, String name, int qua) { this.id = id; this.name = name; this.qua = qua; } public int getId() { return id; } public String getName() { return name; } public int getQua() { return qua; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setQua(int qua) { this.qua = qua; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", qua=" + qua + '}'; } }
4、UserDao
package com.zhangshaopeng.dao; import com.zhangshaopeng.pojo.User; public interface UserDao { User selectById(Integer integer); }
5、Utils工具类
package com.zhangshaopeng.utils; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class MybatisUtils { private static SqlSessionFactory factory=null; static { String config = "mybatis.xml"; try { InputStream inputStream = Resources.getResourceAsStream(config); factory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ SqlSession sqlSession = null; if(factory != null){ sqlSession = factory.openSession(); } return sqlSession; } }
6、UserDao.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="com.zhangshaopeng.dao.UserDao"> <select id="selectById" parameterType="java.lang.Integer" resultType="com.zhangshaopeng.pojo.User"> select * from machain.machain where id = #{id} </select> </mapper>
7、mybatis.xml
<?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> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <environments default="A"> <environment id="A"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="org.gjt.mm.mysql.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/machain?characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="Root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com\zhangshaopeng\dao\UserDao.xml"></mapper> </mappers> </configuration>
8、测试类
package com.zhangshaopeng; import com.zhangshaopeng.dao.UserDao; import com.zhangshaopeng.pojo.User; import com.zhangshaopeng.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class MyTest { @Test public void t(){ SqlSession session = MybatisUtils.getSqlSession(); UserDao dao = session.getMapper(UserDao.class); User user = dao.selectById(1); System.out.println(user); session.close(); } }