在pom.xml文件中配置mybatis坐标和数据库驱动:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
在resources资源目录下新建两个文件和一个文件夹:db.properties、mybtais-config.xml和mapper文件夹
db.properties代码如下:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/croom?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
mybtais-config.xml代码如下:
<?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>
<properties resource="db.properties"></properties> <!-- 数据库信息的配置文件 -->
<settings>
<setting name="cacheEnabled" value="false"/>
</settings>
<typeAliases>
<package name="entity"/> <!-- 实体对象的包名 -->
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<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><!-- 映射的表与实体的配置文件-->
<mapper resource="./mapper/UsersMapper.xml"></mapper>
</mappers>
</configuration>
在src/main/java/entity文件下创建名为:User.java代码如下:
package entity;
public class User {
private Long id;
private String username;
private String password;
private String avater;
private Integer role;
private Integer department;
private String xinqin;
public User(){}
public User(String username , String password, String avatar,Integer department,Integer role,String xinqin){
this.username = username;
this.password = password;
this.avater = avatar;
this.department = department;
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAvatar() {
return avater;
}
public void setAvatar(String avatar) {
this.avater = avatar;
}
public Integer getDepartment() {
return department;
}
public void setDepartment(Integer department) {
this.department = department;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
public String getXinqin(){
return xinqin;
}
public void setXinqin(String xinqin){
this.xinqin=xinqin;
}
}
在src/main/java/mapper文件下创建名为:UsersMapper.java代码如下:
package mapper;
import entity.User;
import java.util.List;
public interface UsersMapper {
/*
* 查询全部用户
* */
List<User> selectAll();
/*
* 用户登录
* */
User checkLogin(String username);
/*
* 添加用户
* */
Integer insertUser(User user);
/*
* 用户名查找
* */
List<String> checkUsername(String username);
/*
* 用户名查找
* */
User loginUserName(String username);
/*
* 通过序号查找用户
* */
User findByUserID(Long id);
/*
* 用户修改
* */
Integer updateByUserId(User user);
/*
* 删除用户
* */
Integer deleteUser(Long id);
}
在src/main/java/serviceImp文件下创建名为:ImpUserMapper.java代码如下:
package server;
import entity.Friend;
import entity.User;
import mapper.UsersMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import util.MybatisUtil;
import java.util.List;
public class ImpUserMapper{
public static SqlSession sqlSession = MybatisUtil.getSqlSession();
public static UsersMapper userMapper = sqlSession.getMapper(UsersMapper.class);
public static List<User> SelectAll(){
List<User> lists = userMapper.selectAll();
return lists;
}
public static User CheckLogin(String username){
User user = userMapper.checkLogin(username);
return user;
}
public static void InsertUser(User user){
userMapper.insertUser(user);
sqlSession.commit();
}
public static void DeleteUser(Long id){
userMapper.deleteUser(id);
sqlSession.commit();
}
public static Boolean checkUsername(String name){
List<String> lists = userMapper.checkUsername(name);
if (lists == null || lists.size() ==0 ) return false;
else return true;
}
public static List<User> checkUserdepart(Integer department){
return userMapper.checkUserdepart(department);
}
public static User loginUserName(String username){
return userMapper.loginUserName(username);
}
public static void updateByUserId(User user){
userMapper.updateByUserId(user);
sqlSession.commit();
}
}
在src/main/java/resources/mapper文件下创建名为:UsersMapper.xml代码如下:
<?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="mapper.UsersMapper">
<select id="selectAll" resultType="User">
select *from users
</select>
<select id="checkUsername" resultType="String" parameterType="String" flushCache="true" useCache="false">
select username from users where username = #{username} LIMIT 1
</select>
<select id="loginUserName" resultType="User" parameterType="String" flushCache="true" useCache="false">
SELECT * FROM users where username=#{username} LIMIT 1
</select>
<select id="checkLogin" resultType="User" flushCache="true" useCache="false">
select *from users where username = #{username} LIMIT 1
</select>
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into users(username,password,avater,department,role,xinqin ) value (#{username},#{password},#{avater},#{department},#{role},#{xinqin})
</insert>
<select id="findByUserID" parameterType="Long" resultType="entity.User">
select * from users where id = #{id} LIMIT 1
</select>
<update id="updateByUserId" parameterType="entity.User">
UPDATE users
<trim prefix="set" suffixOverrides=",">
<if test="username!=null">username=#{username},</if>
<if test="password!=null">password=#{password},</if>
<if test="department!=null">department=#{department},</if>
<if test="role!=null">role=#{role},</if>
<if test="xinqin!=null">xinqin=#{xinqin},</if>
</trim>
WHERE id=#{id}
</update>
<delete id="deleteUser" parameterType="Long">
delete from users where id = #{id}
</delete>
</mapper>
通过测试就可以调通。