1.作用:实现创建一个接口后把mapper.xml由mybatis 生成接口的实现 类,通过调用接口对象就可以获取 mapper.xml 中编写的 sql.
2.mybatis 和 spring 整合时使用的是这个方案.
3.实现步骤:
3.1 创建一个接口
3.1.1 接口包名和接口名与 mapper.xml 中namespace 相同(全路径)
3.1.2 接口中方法名和 mapper.xml 标签的 id 属性相同
3.2 在 mybatis.xml 中使用进行扫描接口和 mapper.xml
4.代码实现步骤:
:
附:实体类:
public class Custom {
public Custom() {
super();
}
private String sname;
private String passwords;
private int age;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getPasswords() {
return passwords;
}
public void setPasswords(String passwords) {
this.passwords = passwords;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Custom [sname=" + sname + ", passwords=" + passwords + ", age=" + age + "]";
}
}
附:数据表:
4.1 在 mybatis01.xml 中下使用
<mappers>
<package name="mapper"/>
</mappers>
全局配置文件如下:
<?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>
<!-- 在mybatis中开启log4j -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 给频繁使用的类名起别名 -->
<typeAliases>
<typeAlias type="pojo.Custom" alias="cu"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- transationManager的type取值:JDBC:使用原生的JDBC进行事务管理/MANAGED
2.MANAGED 把事务管理转交给其他容器(spring); -->
<dataSource type="POOLED">
<!-- dataSource的type取值:
1. POOLED 使用数据库连接池
2. UNPOOLED 不实用数据库连接池,和直接使用 JDBC 一样
3.JNDI :java 命名目录接口技术.
-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/my?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="20182022"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="mapper"/>
</mappers>
</configuration>
4.2 在 mapper 包下新建接口CustomSelectMapper
import java.util.List;
import org.apache.ibatis.annotations.Param;
import pojo.Custom;
public interface CustomSelectMapper {
public List<Custom> selectAll();
//使用注解:mybatis 把参数转换为 map 了,其中@Param("key") 参数内
容就是 map 的 value
public List<Custom> selectCustom(@Param("password")String passwords);
public List<Custom> selectCus(@Param("password")String passwords,@Param("age")int age);
}
4.3 在 mapper 新建一个 CustomSelectMapper.xml(mapper接口和mapper.xml在同一个包(package)下,名字一样(仅后缀不同))
<?xml version="1.0" encoding="UTF-8"?>
<!-- 必须添加dtd文件约束,否则报错 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.CustomSelectMapper">
<select id="selectAll" resultType="cu">
select * from custom
</select>
<select id="selectCustom" resultType="cu">
select * from custom where passwords=#{password}
</select>
<!-- 当多参数时,不需要写parameterType -->
<select id="selectCus" resultType="cu" >
select * from custom where passwords=#{password} and age=#{age}
</select>
</mapper>
4.3.1 namespace 必须和接口全限定路径(包名+类名)一致
4.3.2 id 值必须和接口中方法名相同
4.3.3 如果接口中方法为多个参数,可以省略 parameterType
<select id="selectCus" resultType="cu" >
select * from custom where passwords=#{password} and age=#{age}
</select>
5.多参数实现办法
5.1 在接口中声明方法
public List<Custom> selectAll();//无参
public List<Custom> selectCustom(String passwords);//有参
5.2 在 mapper.xml 中添加sql
5.2.1 也可以在#{}中使用 param1,param2传递参数(上面,我是用注解方式传的参数)
<!-- 当多参数时,不需要写 parameterType -->
<select id="selectCus" resultType="cu" >
select * from custom where passwords=#{param1} and age=#{param2}
</select>
6.可以使用注解方式传参
6.1 在接口中声明方法(形参加注解)
/**
* mybatis 把参数转换为 map 了,其中@Param("key") 参数内
容就是 map 的 value
* @param accin123
* @param accout3454235
* @return
*/
public List<Custom> selectCustom(@Param("password")String passwords);
public List<Custom> selectCus(@Param("password")String passwords,@Param("age")int age);
6.2 在 mapper.xml 中添加注解内容
</select>
<select id="selectCustom" resultType="cu">
select * from custom where passwords=#{password}
</select>
<!-- 当多参数时,不需要写parameterType -->
<select id="selectCus" resultType="cu" >
select * from custom where passwords=#{password} and age=#{age}
</select>
6.2.1 #{} 里面写@Param(“内容”)参数中内容
select * from custom where passwords=#{password}
select * from custom where passwords=#{password} and age=#{age}
测试类:
package service;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
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 mapper.CustomSelectMapper;
import pojo.Custom;
public class Test {
/**
* @param args
* @throws IOException
*/
/**
* @param args
* @throws IOException
*/
/**
* @param args
* @throws IOException
*/
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//读取全局配置文件
InputStream in=Resources.getResourceAsStream("mybatis01.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession session=factory.openSession();
CustomSelectMapper cu=session.getMapper(CustomSelectMapper.class);
/*无参查询
List<Custom> li=cu.selectAll();
for(Custom c:li) {
System.out.println(c.toString());
};*/
//一个参数的查询
List<Custom>li=cu.selectCustom("123");
for(Custom c:li) {
System.out.println(c.toString());
};
/*
List<Custom>li=cu.selectCus("123",21);
for(Custom c:li) {
System.out.println(c.toString());
};*/
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容