1、准备数据库表
1.1、创建数据库表
CREATE TABLE `student` (
`sid` int(11) NOT NULL AUTO_INCREMENT,
`sname` varchar(100) DEFAULT NULL,
`sage` int(11) DEFAULT NULL,
`ssex` varchar(100) DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1.2、插入测试数据
insert into student(sname,sage,ssex) values("joker",18,"男")
2、引入依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
3、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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true"/>
<property name="username" value="root"/>
<property name="password" value="joker123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/StudentMapper.xml"/>
</mappers>
</configuration>
4、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="org.mybatis.example.mapper.StudentMapper">
<select id="selectByName" resultType="org.mybatis.example.bean.Student">
select * from student where sname = #{name,jdbcType=VARCHAR} limit 1
</select>
</mapper>
5、测试
StudentTest.java
package org.mybatis.example;
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.BeforeClass;
import org.junit.Test;
import org.mybatis.example.bean.Student;
import org.mybatis.example.mapper.StudentMapper;
import java.io.InputStream;
public class StudentTest {
static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void beforeClass() throws Exception {
String config = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void selectStudentByName() {
SqlSession sqlSession = sqlSessionFactory.openSession(true);
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectByName("joker");
System.out.println(student);
sqlSession.close();
}
}
6、代码展示
Student.java
package org.mybatis.example.bean;
public class Student {
private Integer sid;
private String sname;
private Integer sage;
private String ssex;
public Student() {
}
public Student(Integer sid, String sname, Integer sage, String ssex) {
this.sid = sid;
this.sname = sname;
this.sage = sage;
this.ssex = ssex;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
@Override
public String toString() {
return "Student{" +
"sid=" + sid +
", sname='" + sname + '\'' +
", sage=" + sage +
", ssex='" + ssex + '\'' +
'}';
}
}
StudentMapper.java
package org.mybatis.example.mapper;
import org.mybatis.example.bean.Student;
public interface StudentMapper {
Student selectByName(String name);
}
7、项目结构