1.先引入pom文件
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.29</version> </dependency> </dependencies> <!--这是方便后面编译时可以带着xml文件--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
2.编写实体类domain.student,定义好字段与getter,setter
package com.bjpowernode.domain; public class Student { private Integer id; private String phone; private String truename; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }
3.定义接口文件dao.studentDao
package com.bjpowernode.dao; import com.bjpowernode.domain.Student; import java.util.List; public interface StudentDao { public List<Student> selectStudents(); }
4.定义 dao.studentDao.xml的mapper文件
<?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.bjpowernode.dao.StudentDao"> <select id="selectStudents" resultType="com.bjpowernode.domain.Student"> select id,phone,truename from user </select> </mapper>
5.定义resources下的mybatis配置文件
<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/daikuan"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/bjpowernode/dao/StudentDao.xml"/> </mappers> </configuration>
6.编写功能测试类Myapp
原文:https://www.cnblogs.com/bing2017/p/14540908.html