mybatis代理模式与非代理模式的区别:
mapper类只需要定义接口,mapper映射文件的namespace的值必须为mapper接口的全类名
书写的SQL的ID 必须与mapper接口中的相对应的方法名相同
配置文件mybatisConfig.xml
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="mybatis.domian"/>
<package name="mybatis.vo"/>
typeAliases>
<environments default="mybatis_1">
<environment id="mybatis_1">
<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="mybatis.mapper">package>
mappers>
configuration>
实体类User
package mybatis.domian;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
public class User implements Serializable{
private Integer id;
private String name;
private String password;
private Float salary;
private Date birthday;
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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 Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(id, user.id) &&
Objects.equals(name, user.name) &&
Objects.equals(salary, user.salary) &&
Objects.equals(birthday, user.birthday);
}
@Override
public int hashCode() {
return Objects.hash(id, name, salary, birthday);
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", salary=" + salary +
", birthday=" + birthday +
'}';
}
}
实体类Department
package mybatis.domian;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Department implements Serializable {
private Integer id;
private String name;
private String location;
private Set set = new HashSet<>();
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
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 getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", name='" + name + '\'' +
", location='" + location + '\'' +
'}';
}
}
mapper接口 UserMapper
package mybatis.mapper;
import mybatis.domian.Department;
import mybatis.domian.User;
import mybatis.vo.UserQueryVo;
import java.util.List;
import java.util.Map;
public interface UserMapper {
void save(User user);
void delete(User user);
void update(User user);
User findById(Integer id);
List<User> findAll();
List<User> findByMap(Map<String,Object> map);
List<User> findQueryVo(UserQueryVo userQueryVo);
Department findDepaetmentByUserName(String name);
Department findDepaetmentByUserName1(String name);
User findUserByName(String name);
}
Service接口
package mybatis.service;
import mybatis.domian.Department;
import mybatis.domian.User;
import java.util.List;
public interface UserService {
void addUser(User user);
List findAllUserList();
User checkLogin(String name,String password);
/**
* 查询某人所在部门
* @param name
* @return
*/
Department findDepaetmentByUserName(String name);
Department findDepaetmentByUserName1(String name);
Department findDepaetmentByUserName2(String name);
}
package mybatis.service;
import mybatis.domian.User;
import java.util.List;
public interface DepartmentService {
List<User> findUserListByDepartmenyName(String name);
List<User> findUserListByDepartmenyName1(String name);
}
Service接口实现类
package mybatis.service.impl;
import mybatis.domian.Department;
import mybatis.domian.User;
import mybatis.mapper.UserMapper;
import mybatis.service.UserService;
import mybatis.util.MybatisUtils;
import mybatis.vo.UserQueryVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class UerServiceImpl implements UserService{
@Override
public void addUser(User user) {
SqlSession sqlSession = null;
if (user == null) {
throw new IllegalArgumentException("");
}
try{
sqlSession = MybatisUtils.getSqlSession();
/*
如何mapper文件使用不是代理模式 则此处需要写成
namespace+方法名
使用的为代理模式时,则可以直接使用方法名
*/
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.save(user);
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtils.clolseSqlSession();
}
}
@Override
public List findAllUserList() {
SqlSession sqlSession = null;
List userList = Collections.emptyList(); // 避免空指针异常
try{
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
userList = mapper.findAll();
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtils.clolseSqlSession();
}
return userList;
}
/**
* 登陆方法
* @param name
* @param password
* @return
*/
@Override
public User checkLogin(String name ,String password) {
SqlSession sqlSession = null;
UserQueryVo userQueryVo = null;
User user = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
userQueryVo = new UserQueryVo();
userQueryVo.setName(name);
userQueryVo.setPassword(password);
List users = mapper.findQueryVo(userQueryVo);
if (CollectionUtils.isNotEmpty(users)){
user = users.get(0);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtils.clolseSqlSession();
}
return user;
}
@Override
public Department findDepaetmentByUserName(String name) {
SqlSession sqlSession = null;
UserQueryVo userQueryVo = null;
Department department = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
department = new Department();
department = mapper.findDepaetmentByUserName(name);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtils.clolseSqlSession();
}
return department;
}
@Override
public Department findDepaetmentByUserName1(String name) {
SqlSession sqlSession = null;
UserQueryVo userQueryVo = null;
Department department = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
department = new Department();
department = mapper.findDepaetmentByUserName1(name);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtils.clolseSqlSession();
}
return department;
}
@Override
public Department findDepaetmentByUserName2(String name){
SqlSession sqlSession = null;
UserQueryVo userQueryVo = null;
User user = null;
try {
sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
user = new User();
user = mapper.findUserByName(name);
System.out.println(user);
if (user != null){
return user.getDepartment();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtils.clolseSqlSession();
}
return null;
}
}
package mybatis.service.impl;
import mybatis.domian.Department;
import mybatis.domian.User;
import mybatis.mapper.DepartmentMapper;
import mybatis.service.DepartmentService;
import mybatis.util.MybatisUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class DepartmentServiceImpl implements DepartmentService {
@Override
public List findUserListByDepartmenyName(String name) {
List userList = Collections.emptyList();
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtils.getSqlSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
userList = mapper.findUserListByDepartmenyName(name);
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtils.clolseSqlSession();
}
return userList;
}
@Override
public List findUserListByDepartmenyName1(String name) {
List userList = new ArrayList<>();
Department department = null;
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtils.getSqlSession();
DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
department = mapper.findByName(name);
if (department != null){
Set userSet = department.getSet();
// 将 userSet中的数据 全部加入到userList
CollectionUtils.addAll(userList, userSet.iterator());
}
}catch (Exception e){
e.printStackTrace();
}finally {
MybatisUtils.clolseSqlSession();
}
return userList;
}
}
UserQueryVo
public class UserQueryVo extends User {
}
userMapper.xml
<mapper namespace="mybatis.mapper.UserMapper">
<insert id="save" parameterType="User">
INSERT INTO
mybatis_user(user_name,user_password,user_salary,user_birthday)
VALUE
(#{name},#{password},#{salary},#{birthday})
insert>
<select id="findAll" resultType="User">
SELECT
user_id id ,
user_name name,
user_password password ,
user_salary salary ,
user_birthday birthday
FROM
mybatis_user
select>
<select id="findQueryVo" resultType="User" parameterType="UserQueryVo">
SELECT
user_id id ,
user_name name,
user_password password ,
user_salary salary ,
user_birthday birthday
FROM
mybatis_user
WHERE
1=1
<if test="name != null and name != '' ">
AND user_name LIKE "%" #{name} "%"
if>
<if test="password != null and password != ''">
AND user_password LIKE "%" #{password}"%"
if>
<if test="salary != null and salary != '' ">
AND user_salary LIKE "%" #{salary} "%"
if>
<if test="birthday != null and birthday != ''">
AND user_birthday LIKE "%" #{birthday} "%"
if>
select>
<select id="findDepaetmentByUserName" parameterType="java.lang.String" resultType="Department">
SELECT
d.department_id id,
d.department_name name,
d.department_location location
FROM
mybatis_user u
LEFT JOIN
mybatis_department d
ON
u.department_id = d.department_id
WHERE
u.user_name = #{name}
select>
<resultMap id="DepartmentRM" type="Department">
<id property="id" column="department_id"/>
<result property="name" column="department_name"/>
<result property="location" column="department_location"/>
resultMap>
<select id="findDepaetmentByUserName1" parameterType="java.lang.String" resultMap="DepartmentRM">
SELECT
d.department_id ,
d.department_name ,
d.department_location
FROM
mybatis_user u
LEFT JOIN
mybatis_department d
ON
u.department_id = d.department_id
WHERE
u.user_name = #{name}
select>
<resultMap id="UserRM" type="User">
<id property="id" column="user_id"/>
<result property="name" column="user_name"/>
<result property="password" column="user_password"/>
<result property="salary" column="user_salary"/>
<result property="birthday" column="birthday"/>
<association property="department" column="depaetment_id" javaType="Department">
<id property="id" column="department_id"/>
<result property="name" column="department_name"/>
<result property="location" column="department_location"/>
association>
resultMap>
<select id="findUserByName" parameterType="java.lang.String" resultMap="UserRM">
SELECT
u.user_id,
u.user_name,
u.user_salary,
u.user_password,
u.user_birthday,
d.department_id ,
d.department_name ,
d.department_location
FROM
mybatis_user u
INNER JOIN
mybatis_department d
ON
u.department_id = d.department_id
WHERE
u.user_name = #{name}
select>
mapper>
Department.xml
<mapper namespace="mybatis.mapper.DepartmentMapper">
<select id="findUserListByDepartmenyName" parameterType="java.lang.String" resultType="User">
SELECT
u.user_id id ,
u.user_name name,
u.user_salary salary ,
u.user_password password ,
u.user_birthday birthday
FROM
mybatis_user u
INNER JOIN
mybatis_department d
ON
u.department_id = d.department_id
WHERE
d.department_name = #{name}
select>
<resultMap id="DepartmentRM" type="Department">
<id property="id" column="department_id"/>
<result property="name" column="department_name"/>
<result property="location" column="department_location"/>
<collection property="set" column="depaetment_id" ofType="User">
<id property="id" column="user_id"/>
<result property="name" column="user_name"/>
<result property="password" column="user_password"/>
<result property="salary" column="user_salary"/>
<result property="birthday" column="user_birthday"/>
collection>
resultMap>
<select id="findByName" resultMap="DepartmentRM">
SELECT
u.user_id,
u.user_name,
u.user_salary,
u.user_password,
u.user_birthday,
d.department_id ,
d.department_name ,
d.department_location
FROM
mybatis_user u
INNER JOIN
mybatis_department d
ON
u.department_id = d.department_id
WHERE
d.department_name = #{name}
select>
mapper>
重点内容
collection: 集合 表示多的一方,比如一个部门中有多 个员工,其中property表示所映射的属性名,column该属性在数据库中的列名,ofType该属性的类型名称
association: 有一个 比如:一个员工对应一个部门property、column与collection中属性相同,javaType属性的类型名称
resultType: 表示结果类型,当数据库字段名与实体字段名相同时可直接使用实体类名
resultMap: 当数据库字段与实体字段不同,且SQL 对查询字段并未起表明时, 需要对结果进行映射
方法思想,在做条件查询时,创建了UserQueryVo实体类 ,通过集成实体类,得到实体的属性,实现对查询条件的封装
PS: 仅日常笔记整理,有错误请指出,喷子绕路