pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>org.examplegroupId>
<artifactId>MyBaitsartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<name>MyBaitsname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
dependencies>
project>
User实体类
package com.zhnx.demo1.pojo;
import lombok.Data;
/**
* ssm.t_user
*/
@Data
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String gender;
private String email;
public User() {
}
public User(Integer id, String username, String password, Integer age, String gender, String email) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
this.gender = gender;
this.email = email;
}
}
补充:使用@Data注解需要在pom.xml中配置lombok的依赖
lombok依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.26version>
<scope>compilescope>
dependency>
核心配置文件主要功能,与数据库和映射文件进行绑定
mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="com.zhnx.demo1.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.zhnx.demo1.mapper"/>
mappers>
configuration>
package com.zhnx.demo1.mapper;
import com.zhnx.demo1.pojo.User;
public interface UserMapper {
/**
* 根据用户名查询用户信息
* @param name
* @return
*/
User getUserByUsername(String name);
}
在映射文件中写SQL语句,操作数据库
UserMapper.xml
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhnx.demo1.mapper.UserMapper">
<insert id="insertUser">
insert into t_user values (null,'admin','00000000',23,'男','admin000.qq.com');
insert>
mapper>
在pom.xml中加入log4j的依赖
<!-- log4j日志 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
eg:
1、在UserMapper接口中新增方法
public interface UserMapper{ User selectUser(); }
2、在UserMapper.xml中配置SQL语句等
<select id="selectUser" resultType="User"> select * from t_user where id = 1; </select>
resultType:设置返回的结果类型,就是查询得到数据转换成的对应的Java类型(全类名)
可以在核心配置文件中使用标签设置类型别名
<typeAliases>
<package name="com.zhnx.demo1.pojo"/>
typeAliases>
resultMap:自定义映射处理多对一或者一对多映射关系
字符串拼接
注意添加单引号
占位符赋值
不需要添加单引号
可以防止SQL注入
${}和#{}都可以,以任意键值就可以获取参数值
需要注意的是,${}需要手动添加单引号
不可以使用#{username}、#{password}的方式
因为 因为当mapper接口中的方法参数为多个时,此时MyBatis会自动将这些参数放在一个map集合中,以arg0,arg1…或param1,param2…为键,以参数为值
所以,多个字面量参数的处理方式应该为==#{arg0}…或‘${arg0}’==…
这种参数可以直接#{==map键值对的键==值}
这种参数可以直接==通过实体类的属性名==获取属性值
* 查询出来的多个Map集合统一放在list集合中
** List<Map<String, Object>> getAllUserToMap();
** 查询结果: {password=123456, sex=男 ,
id=1, age=23, username=admin
}
* 查询出来的多个Map集合放在Map集合中(Map中嵌套Map)
** 使用@MapKey("id")为外层的Map集合指定键值
** @MapKey("id")
Map<String, Object> getAllUserToMap();
** 查询结果:{
1={password=123456, sex=男, id=1, age=23, username=admin},
2={password=123456, sex=男, id=2, age=23, username=张三},
3={password=123456, sex=男, id=3, age=23, username=张三}
}
LIKE ‘abc%’
具体情况根据SQL语句来判断
delete from t_user where id in (${ids})
select * from ${tableName}
//useGeneratedKeys=“true”
//keyProperty="id"
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{username},#{password},#{age},#{sex})
</insert
//resultMap标签的和其中属性的详解
<!--
resultMap:设置自定义映射
属性:
id:表示自定义映射的唯一标识
type:查询的数据要映射的实体类的类型
子标签:
id:设置主键的映射关系
result:设置普通字段的映射关系association:设置多对一的映射关系
collection:设置一对多的映射关系
属性:
property:设置映射关系中实体类中的属性名
column:设置映射关系中表中的字段名-->
<!--简单例子 --!>
<resultMap id="userMap" type="User">
<id property="id" column="id"></id>
<result property="userName" column="user_name"></result>
<result property="password" column="password"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</resultMap>
<select id="selectUser" resultMap="userMap">
select * from user;
<select>
例子:实体类中还有实体类
级联:使用 “类名.字段名” 的方式进行映射
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<result column="did" property="dept.did"></result>
<result column="dname" property="dept.dname"></result>
</resultMap>
association
<resultMap id="empDeptMap" type="Emp">
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="age" property="age"></result>
<result column="sex" property="sex"></result>
<association property="dept" javaType="Dept">
<id column="did" property="did"></id>
<result column="dname" property="dname"></result>
</association>
</resultMap>
例子:实体类中有集合
collection
<resultMap id="deptEmpMap" type="Dept">
<id property="did" column="did"></id>
<result property="dname" column="dname"></result>
<collection property="emps" ofType="Emp">
<id property="eid" column="eid"></id>
<result property="ename" column="ename"></result>
<result property="age" column="age"></result>
<result property="sex" column="sex"></result>
</collection>
</resultMap>