第一个MyBtis项目
Maven:pom.xml所需依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
idea使用maven创建Mybatis项目操作持久层时,常遇到资源导出失败问题,(解决方法)
在maven的pom.xml文件中添加
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
创建环境
- pojo(实体类) 根据数据库中的数据类型自行更改
public class Student {
private int id;
private String name;
private int age;
private int sex;
public Student() {
}
public Student(int id, String name, int age, int sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
核心配置
public interface StudentDao {
List<Student> getStudentList();
}
- 与Dao相对应的Mapper文件:StudentMapper.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.xzb.dao.StudentDao">
<select id="getStudentList" resultType="com.xzb.pojo.Student">
select * from student
</select>
</mapper>
<?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/stu?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/xzb/dao/StudentMapper.xml"/>
</mappers>
</configuration>
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
String resource = "config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e){
}finally {
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
测试
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = null;
try {
sqlSession = MyBatisUtil.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> studentList = studentDao.getStudentList();
for (Student student : studentList) {
System.out.println(student);
}
}catch (Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
注意事项:
-
mapper配置文件必须在config核心配置文件中注册
-
绑定接口mapper文件中的<mapper namespace="接口全路径>
-
绑定接口中的方法名<select id="接口中的方法名"></select>
-
返回值类型<select resultType="返回值的类型的全类名"></select>
-
Maven导出时需配置resource防止导出失败报错ExceptionInInitializerError