<?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="cn.javabs.job.mapper.UserMapper">
<!--添加用户-->
<insert id="addUser" parameterType="cn.javabs.job.entity.User">
insert into user
<trim prefix="(" suffix=") values" suffixOverrides=",">
<if test="username != nul and username != '' ">
username,
</if>
<if test="password != nul and password != '' ">
password,
</if>
<if test="headPic != nul and headPic != '' ">
headPic,
</if>
<if test="sex != nul and sex != '' ">
sex,
</if>
<if test="email != nul and email != '' ">
email,
</if>
<if test="mobile != nul and mobile != '' ">
mobile,
</if>
<if test="degree != nul and degree != '' ">
degree,
</if>
<if test="workExp != nul and workExp != '' ">
workExp,
</if>
<if test="type != nul and type != '' ">
type,
</if>
<if test="selfDescription != nul and selfDescription != '' ">
selfDescription,
</if>
</trim>
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="username != nul and username != '' ">
#{username},
</if>
<if test="password != nul and password != '' ">
#{password},
</if>
<if test="headPic != nul and headPic != '' ">
#{headPic},
</if>
<if test="sex != nul and sex != '' ">
#{sex},
</if>
<if test="email != nul and email != '' ">
#{email},
</if>
<if test="mobile != nul and mobile != '' ">
#{mobile},
</if>
<if test="degree != nul and degree != '' ">
#{degree},
</if>
<if test="workExp != nul and workExp != '' ">
#{workExp},
</if>
<if test="type != nul and type != '' ">
#{type},
</if>
<if test="selfDescription != nul and selfDescription != '' ">
#{selfDescription},
</if>
</trim>
</insert>
<!--修改用户-->
<update id="editUser" parameterType="cn.javabs.job.entity.User">
update user
<set>
<!-- <if test="username != nul and username != '' ">
#{username},
</if>-->
<if test="password != nul and password != '' ">
#{password},
</if>
<if test="headPic != nul and headPic != '' ">
#{headPic},
</if>
<if test="sex != nul and sex != '' ">
#{sex},
</if>
<if test="email != nul and email != '' ">
#{email},
</if>
<if test="mobile != nul and mobile != '' ">
#{mobile},
</if>
<if test="degree != nul and degree != '' ">
#{degree},
</if>
<if test="workExp != nul and workExp != '' ">
#{workExp},
</if>
<!-- <if test="type != nul and type != '' ">
#{type},
</if>-->
<if test="selfDescription != nul and selfDescription != '' ">
#{selfDescription},
</if>
</set>
where
userId = #{userId}
</update>
<!--删除用户-->
<delete id="delUser" parameterType="int">
delete from user
<where>
<if test="userId != nul and userId != '' ">
and userId = #{userId},
</if>
</where>
</delete>
<!--查询所有用户-->
<select id="findAllUserList" resultType="cn.javabs.job.entity.User">
select * from user
</select>
<!--按照邮箱查询用户的信息-->
<select id="findUserByEmail" resultType="cn.javabs.job.entity.User" parameterType="String">
select * from user
<where>
<if test="email != nul and email != '' ">
and email = #{email},
</if>
</where>
</select>
<!--按照条件(用户名或者性别或者学历)查询用户信息 返回的集合-->
<select id="findUserByCondition" resultType="cn.javabs.job.entity.User" parameterType="String">
select * from user
<where>
<!--用户名 进行模糊查询-->
<if test="username != nul and username != '' ">
and username like concat('%',#{username},'%'),
</if>
<if test="sex != nul and sex != '' ">
and sex = #{sex},
</if>
<if test="degree != nul and degree != '' ">
and degree = #{degree},
</if>
</where>
</select>
<!--根据用户编号查询用户信息-->
<select id="findUserByUserId" resultType="cn.javabs.job.entity.User" parameterType="int">
select * from user
<where>
<if test="userId != nul and userId != '' ">
and userId = #{userId}
</if>
</where>
</select>
<!--用户登录-->
<select id="login" resultType="cn.javabs.job.entity.User" parameterType="cn.javabs.job.entity.User">
select * from user
<where>
<if test="username != nul and username != '' ">
and username = #{username}
</if>
<if test="password != nul and password != '' ">
and password = #{password}
</if>
<if test="type != nul and type != '' ">
and type = #{type}
</if>
</where>
</select>
</mapper>
6. 编写业务逻辑层接口
package cn.javabs.job.service;
import cn.javabs.job.entity.User;
import java.util.List;
public interface UserService {
List<User> getAllUserList();
User getUserByEmail(String email);
User getUserByUserId(int userId);
List<User> getUserByCondition(String condition);
User userLogin(User user);
int deleteUser(int userId);
int editUser(User user);
int addUser(User user);
}
7. 编写业务逻辑层接口的实现类
package cn.javabs.job.service.impl;
import cn.javabs.job.entity.User;
import cn.javabs.job.mapper.UserMapper;
import cn.javabs.job.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Resource
@Qualifier("userMapper")
private UserMapper userMapper;
@Override
public List<User> getAllUserList() {
return userMapper.findAllUserList();
}
@Override
public User getUserByEmail(String email) {
return userMapper.findUserByEmail(email);
}
@Override
public User getUserByUserId(int userId) {
return userMapper.findUserByUserId(userId);
}
@Override
public List<User> getUserByCondition(String condition) {
return userMapper.findUserByCondition(condition);
}
@Override
public User userLogin(User user) {
return userMapper.login(user);
}
@Override
public int deleteUser(int userId) {
return userMapper.delUser(userId);
}
@Override
public int editUser(User user) {
return userMapper.editUser(user);
}
@Override
public int addUser(User user) {
return userMapper.addUser(user);
}
}
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。