IDEA创建MyBatis项目--实现简单的查操作
IDEA创建MyBatis项目--实现简单的查操作
1.创建一个maven工程,不使用模板
2.通过maven加载Mybatis依赖包
在pom文件中导入maven坐标
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
3.新建config.properties配置文件
填写内容
4.新建Mybatis配置文件
在以下位置创建
命名为mybatis-config.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>
<properties resource="config.properties"/>
<typeAliases>
<package name=""/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
5.编写SqlSessionFactoryUtils工具类
在以下位置创建
没有的包右击创建package
package com.Utils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
import java.io.IOException;
import java.io.InputStream;
public class SqlSessionFactoryUtils {
private final static Class<SqlSessionFactoryUtils> lock=SqlSessionFactoryUtils.class;
private static SqlSessionFactory sqlSessionFactory=null;
private SqlSessionFactoryUtils(){};
public static SqlSessionFactory getSqlSessionFactory(){
synchronized (lock){
if (sqlSessionFactory!=null){
return sqlSessionFactory;
}
String resource="mybatis-config.xml";
InputStream inputStream;
try {
inputStream=Resources.getResourceAsStream(resource);
sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
}catch (IOException e){
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession(){
if (sqlSessionFactory==null){
getSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
6.编写Student实体类
package com.entity;
public class Student {
private int id;
private String name;
private int age;
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;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
7.编写StudentMapper接口
package com.mapper;
import com.entity.Student;
public interface StudentMapper {
public Student getStudent(long id);
}
8.编写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.mapper.StudentMapper">
<select id="getStudent" parameterType="long" resultType="com.entity.Student">
select id,name,age from student where id=#{id}
</select>
</mapper>
9.与数据库表结构对应
10.编写Main类(或测试类test)
package com;
import com.entity.Student;
import com.mapper.StudentMapper;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
public class Main {
public static void main(String[] args){
SqlSession sqlSession=SqlSessionFactoryUtils.openSqlSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
System.out.println(studentMapper.getStudent(1).toString());
}
}
注意点
1.Mapper.xml的namespace与Mapper.java类的包名要一致
2.Mapper.xml中select元素的id属性值必须跟Mapper.java中方法名要一致
3.把xml文件放在resources目录下