一、中文学习文档链接地址
https://mybatis.org/mybatis-3/zh/getting-started.html
二、准备
- 要使用myBatis,要导入两个依赖,一个是myBatis包,一个是myBatis驱动包。
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.6</version>
</dependency>
或者
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
- 数据库URL:
jdbc:mysql://localhost:3306/stu?useSSL=true&useUnicode=true&serverTimezone=GMT
- XML文件里的结果集映射
<!--这里的id对应select里resultMap,resultMap里是什么,这里的id就是什么-->
<resultMap id="UserMap" type="User">
<!-- column为数据库里的字段名,property为实例类中的属性名-->
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserById" resultMap="UserMap"> <!--这里的resultMap随意取-->
select * from user where id=#{id}
</select>
4.MybatisUtil工具类
public class MybatisUtil {
private static SqlSessionFactory factory = null;
static {
String config = "mybatis.xml";
try{
InputStream inputStream = Resource.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}catch(IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession session = null;
if(factory != null){
factory.openSession();
}
return session;
}
}
- 分页
<!--startIndex 是数据的下标 pageSize是显示多少条数据-->
<select id="findAllMap" parameterType="map" resultType="pojo.User">
select * from user limit #{startIndex},#{pageSize}
</select>
\\Dao层方法:
List<User> findAllMap(Map<String,Integer> map);
测试:
public class MyTest {
public static void main(String[] args) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
// List<User> userList = mapper.findAll();
// for (User user :
// userList) {
// System.out.println(user);
// }
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("startIndex",2);
map.put("pageSize",4);
List<User> userList = mapper.findAllMap(map);
for (User user :
userList) {
System.out.println(user);
}
sqlSession.close();
}
}
- 多对一
第一种:
<!-- StudentTeacher随便取-->
<select id="findAll" resultMap="StudentTeacher">
select * from student
</select>
<!--StudentTeacher要对应上面随便取的名字-->
<resultMap id="StudentTeacher" type="pojo.Student">
<!--getTeacher要对应下面随便取的名字,tid是副表当中对应主表id的字段-->
<association property="teacher" column="tid" javaType="pojo.Teacher" select="getTeacher"></association>
</resultMap>
<select id="getTeacher" resultType="pojo.Teacher"> <!--getTeacher随便取-->
select * from teacher where id=#{id}
</select>
第二种:
<select id="findAllOne" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id
</select>
<resultMap id="StudentTeacher2" type="pojo.Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<association property="teacher" javaType="pojo.Teacher">
<result property="name" column="tname"></result>
</association>
</resultMap>
第三种分部:
// mapper层:
// 通过分步查询员工以及员工所对应的部门信息
// 分步查询第一步:查询员工信息
Emp getEmpAndDeptByStepOne(@Param("uid")String uid);
// 分步查询第二步:查询部门信息
Dept getEmpAndDeptStepTwo(@Param("did")String did);
// mapper.xml:
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="uid" column="uid"/>
<result property="empName" column="emp_name"/>
// 不是驼峰命名的属性可以不写,它自动就映射了
<result property="age" column="age"/>
<result property="sex" column="sex"/>
// 这里的dept是Emp实体类中的属性,类型为Dept
<association property="dept" select="getEmpAndDeptStepTwo" column="did"></association>
</resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where uid = #{uid}
</select>
<select id="getEmpAndDeptStepTwo" resultType="Dept">
select * from t_dept where did = #{did}
</select>
- 模糊查询
(1) Java代码执行的时候,传递通配符 % %
List<User> userList = mapper.getUserLike("%李%"); //查询姓李的人
(2) 在sql拼接中使用通配符!
select * from user where name like "%" #{value} "%";
8.关于 “#” 和 “$” 的使用
因为 “$” 不是安全的,会造成sql注入,所以一般查询条件使用 “#”,后者是直接将字符串原模原样映射到sql语句中的,例如:
传的参数是:
public User getUser(int id);
写的sql语句是:
<select id="getUser">
select * from user order by ${id} desc
</select>
实际上输出的sql语句是:
select * from user order by id desc
所以我们可以把它用于动态给数据库表复制,比如:
传的参数是:
public User getUser(int id);
写的sql语句是:
<select id="getUser">
select * from user order by ${id} desc
</select>
实际上输出的sql语句是:
select * from user order by id desc
9、新增数据之后自动获取数据主键
// 说明:
// useGenerateKeys:开启自动获取主键功能
// keyProperty:将获取的主键用什么属性接收(获取主键id,肯定是用id来接收了)
<insert id="xxxx" useGenerateKeys="true" keyProperty="id"></insert>