打开MySQL,创建数据库创建一个学生表
导入如下jar包(mybatis所需jar包,连接MySQL所需jar包,单元测试类所需jar包)
package com.mybatis.pojo;
public class Student {
private Integer id;
private String username;
private String password;
private Integer role;
/* public Student(Integer id, String username, String password, Integer role) {
this.id = id;
this.username = username;
this.password = password;
this.role = role;
}*/
@Override
public String toString() {
return
"{id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", role=" + role+ "}";
}
}
2.创建dao数据层StudentMapper学生映射类
要拿什么数据做什么
package com.mybatis.dao;
import com.mybatis.pojo.Student;
public interface StudentMapper {
public Student queryStudent(int role);
}
3.创建test包、resources包,在项目根目录下创建并且配置好路径(这样就方便了写文件的路径)
4.在resources文件夹下创建MybatisConfig.xml配置文件
- 一定要有文件头
- 连接MySQL数据库
- 映射dao
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/yeujuan?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="StudentMapper.xml"/>
mappers>
configuration>
5.在resources文件夹下创建StudentMapper.xml文件(文件名要和dao层类名一致)
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.StudentMapper">
<select id="queryStudent" resultType="com.mybatis.pojo.Student">
SELECT `id`, `username`, `password`,`role` FROM `user` WHERE `role` = #{role}
select>
mapper>
6.测试,在test文件夹中创建StudentTest测试类
import com.mybatis.dao.StudentMapper;
import com.mybatis.pojo.Student;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class StudentTest {
@Test
public void studentTest(){
SqlSession sqlSession=getSqlSession(); //SQLSession是线程不安全的,要及时关闭
try {
// 1.这个写法不推荐, 因为没有使用映射接口, 只是用了映射的配置文件
List<Student> student=sqlSession.selectList("com.mybatis.dao.StudentMapper.queryStudent",2);
// 2.推荐这种写法, 也是使用mybatis 绝大部分写得最多的语句
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student1 = mapper.queryStudent(2);
System.out.println(student1);
}finally {
sqlSession.close();
}
}
public SqlSession getSqlSession(){
//拿到SqlSessionFactory 工厂对象
//1. 拿到全局配置文件的路径跟文件名
String mybatisConfigFile = "MybatisConfig.xml";
try {
//2. 把1. 用Resources getResourceAsStream 方法取得一个输入流的对象
InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);
//3. 把2. 用来实例化SqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
import com.mybatis.pojo.Student;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class StudentTest {
@Test
public void studentTest(){
SqlSession sqlSession=getSqlSession();
//由于根据role查询返回的记录数可能超过1,这里我们采用list集合装进来
List<Student> student=sqlSession.selectList("com.mybatis.dao.StudentMapper.queryStudent",2);//参1:dao层映射类的方法全路径;参2:传入的参数值
System.out.println(student);
}
//封装获取SqlSeesion对象方法
public SqlSession getSqlSession(){
//拿到SqlSessionFactory 工厂对象
//1. 拿到全局配置文件的路径跟文件名
String mybatisConfigFile = "MybatisConfig.xml";
try {
//2. 用Resources getResourceAsStream 方法取得一个输入流的对象
InputStream inputStream = Resources.getResourceAsStream(mybatisConfigFile);
//3. 用来实例化SqlSessionFactory 对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory.openSession();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
}
结果:
另外,我们可以配置好日志的文件,在resources文件夹下创建log4j.xml文件
DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration debug="true">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
layout>
appender>
<logger name="java.sql">
<level value="debug" />
logger>
<logger name="org.apache.ibatis">
<level value="info" />
logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
root>
log4j:configuration>
就可以得到结果图里面的日志了
全局配置文件补充
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="dbconfig.properties">properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<package name="com.mybatis.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.mybatis.dao"/>
mappers>
configuration>
dbconfigproperties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/yeujuan?useSSL=false
username=root
password=root
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)