总配置文件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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--配置数据源:创建Connection对象-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/bjpowernode?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</dataSource>
</environment>
</environments>
<!--指定其他mapper文件的位置
找到其他mapper文件的目的是找到sql语句
使用注意:
resource="mapper.文件的路径,用/分割"-->
<mappers>
<mapper resource="Dao/StudentMapper.xml"/>
</mappers>
</configuration>
3.1 使用Dao代理
运用dao代理的条件:
mapper文件中的namespace必须是dao接口的全限定名称
并且子标签中id属性必须要是dao接口里的方法名称
dao代理的含义:创建dao接口的一个实现类,重写dao接口方法,创建实现类对象
dao代理的实现:利用sqlSession的getMapper(返回类型的全限定名称)
3.2 参数传递的3个情况
package Dao;
import Para.ParaQuery;
import domain.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface StudentDao {
/*
单个简单类型(基本类型+String)的参数传递:
*/
Student SelectById(Integer id);
/*
多个简单类型(基本类型+String)的参数传递:
*/
List<Student> SelectByIdOrName(@Param("StudentId") Integer id,@Param("StudentName") String name);
/*
实体类类型对象(基本类型+String)的参数传递:
*/
int InsertStudent(Student student);
List<Student> SelectByParaQuery(ParaQuery paraQuery);
}
3.3 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="Dao.StudentDao">
<!--
mapper是根标签
mapper里面可以写<insert>,<update>,<select>等标签
标签的id属性:正常是需要重写的方法名
标签的resultType属性:将数据赋值给哪个类型的Java对象
标签内部都有同名的sql语句
-->
<!--
单个简单类型(基本类型+String)的参数传递:
-->
<select id="SelectById" parameterType="Integer" resultType="domain.Student">
select id,name,email,age from student where id=#{RandomName}
</select>
<!--
多个简单类型(基本类型+String)的参数传递:
-->
<select id="SelectByIdOrName" resultType="domain.Student">
select id,name,email,age from student where id=#{StudentId} or name=#{StudentName}
</select>
<!--
实体类类型对象的参数传递:
-->
<insert id="InsertStudent" parameterType="domain.Student">
insert into student values(#{id},#{name},#{email},#{age})
</insert>
<select id="SelectByParaQuery" resultType="domain.Student">
select id,name,email,age from student where id=#{p1} or name=#{p2 }
</select>
</mapper>
3.4 实体类Student
package domain;
public class Student {
private Integer id;
private String name;
private String email;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "学生实体{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
封装类ParaQuery
package Para;
public class ParaQuery {
private Object p1;
private Object p2;
public Object getP1() {
return p1;
}
public void setP1(Object p1) {
this.p1 = p1;
}
public Object getP2() {
return p2;
}
public void setP2(Object p2) {
this.p2 = p2;
}
}
3.5 工具类MyBatisUtil
package utils;
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 java.io.IOException;
import java.io.InputStream;
//工具类:先创建方法创建SqlSessionFactory
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
String config="mybatis-config.xml";
try{
InputStream inputStream = Resources.getResourceAsStream(config);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch(IOException e){
e.printStackTrace();
}
}
//创建方法,返回值是SqlSession对象sqlSession
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
3.6 Test测试类
package Dao;
import Para.ParaQuery;
import domain.Student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;
import java.util.List;
//运用dao代理进行单参数传递
public class MyTest {
@Test
public void testSelectById() {
//1.获取SqlSession对象
SqlSession sqlSession = MyBatisUtil.getSqlSession();
//2.获得dao的实现类对象
StudentDao mapper = sqlSession.getMapper(StudentDao.class);
//3.调用方法
Student student = mapper.SelectById(1);
System.out.println("查询的结果===" + student);
//4.关闭sqlSession对象
sqlSession.close();
}
//运用dao代理进行多参数传递
@Test
public void testSelectByIdOrName(){
//1.获取sqlSession对象
SqlSession sqlSession=MyBatisUtil.getSqlSession();
//2.获取dao的实现类对象
StudentDao mapper=sqlSession.getMapper(StudentDao.class);
//3.调用方法,实现SQL语句
List<Student> list = mapper.SelectByIdOrName(1, "陆婷婷");
for(Student student:list){
System.out.println(student);
}
sqlSession.close();
}
//运用dao代理进行实体类对象传递:实质是将传递多个参数封装成对象传递
@Test
public void testInsertStudent(){
//1.获取sqlSession对象
SqlSession sqlSession=MyBatisUtil.getSqlSession();
//2.获取dao的实现类对象
StudentDao mapper=sqlSession.getMapper(StudentDao.class);
//3.调用方法,实现SQL语句
Student student=new Student();
student.setId(4);
student.setName("周莫");
student.setEmail("zm@qq.com");
student.setAge(17);
int rows = mapper.InsertStudent(student);
//DML语句要手动提交
sqlSession.commit();
System.out.println("影响的行数:"+rows);
sqlSession.close();
}
//运用dao代理进行自定义类的对象传递:实质是将传递多个参数封装成对象传递
@Test
public void testSelectByParaQuery(){
//1.获取sqlSession对象
SqlSession sqlSession=MyBatisUtil.getSqlSession();
//2.获取dao的实现类对象
StudentDao mapper=sqlSession.getMapper(StudentDao.class);
//3.调用方法,实现SQL语句
ParaQuery p=new ParaQuery();
p.setP1(2);
p.setP2("张磊");
List<Student> list = mapper.SelectByParaQuery(p);
for(Student student:list){
System.out.println(student);
}
sqlSession.close();
}
}
3.7 测试结果
//testSelectById
查询的结果===学生实体{id=1, name='张磊', email='zl@163.com', age=23}
//testSelectByIdOrName
学生实体{id=1, name='张磊', email='zl@163.com', age=23}
学生实体{id=2, name='陆婷婷', email='luu@163.com', age=22}
//testInsertStudent
navicate查看,已经添加了相关信息
//testSelectByParaQuery
学生实体{id=1, name='张磊', email='zl@163.com', age=23}
学生实体{id=2, name='陆婷婷', email='luu@163.com', age=22}