file->New->Project
选择Maven项目
编写pom.xml文件
org.springframework.boot
spring-boot-starter-parent
2.3.4.RELEASE
org.springframework.boot
spring-boot-starter
com.baomidou
mybatis-plus-boot-starter
3.4.3
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-thymeleaf
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.12
mysql
mysql-connector-java
8.0.23
com.alibaba
druid
1.1.21
junit
junit
4.12
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-validation
com.auth0
java-jwt
3.13.0
编写application.yml
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
# 3306为数据库端口号,demo4为数据库表名
url: jdbc:mysql://localhost:3306/demo4?characterEncoding=utf-8
username: root #数据库用户名
password: 123456 #数据库密码
type: com.alibaba.druid.pool.DruidDataSource
maxActive: 50
maxWait: 2000
minIdle: 10
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.project.bean #对应项目结构
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
type-aliases-package: com.project.bean #对应项目结构
数据库设计
建立包com.project,在此包下建立bean、dao、service包,分别建立用户实体bean,持久层,业务层等,以及在resources目录下下创建映射文件 持久层名.xml
创建结束后的结构
package com.project.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("t_user")//当前实体对应的数据库表名称
public class UserBean {
//@TableId注解,指定属性对应主键的列
//type = IdType.AUTO使用数据库表主键自动高增长策略
@TableId(value = "pk_userid",type = IdType.AUTO)
private int userid;
//@TableField普通属性,书写属性对应的数据表的列
@TableField("f_username")
private String username;
@TableField("f_password")
private String password;
public UserBean() {
}
public UserBean(String username, String password) {
this.username = username;
this.password = password;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
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;
}
@Override
public String toString() {
return "UserBean{" +
"userid=" + userid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.project.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.bean.UserBean;
import org.springframework.stereotype.Repository;
/**
* 持久接口
* 1.需要继承BaseMapper接口
* 2.在接口里书写对应的实体类类名
* 3.加上注解@Repository,表示该接口为持久层组件
*/
@Repository
public interface UserDao extends BaseMapper {
//此时持久接口中,已经包含基本单表的增删查改的实现
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
List findByItem(@Param("name")String name,
@Param("start")LocalDate start,
@Param("end") LocalDate end);
/**
* 根据姓名分页查询
* @param page
* @param name
* @return
*/
IPage pageByItem(Page page,
@Param("name")String name);
}
package com.project.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.project.bean.UserBean;
import java.time.LocalDate;
import java.util.List;
/**
* 自定义业务层接口
* 1.需要继承IService
* 注意:通用service接口和通用mapper接口不一样,
* 通用mapper接口不需要提供实现类,就已经包含所有数据的数据库操作方法
* 通用service接口必须要提供实现类
*/
public interface UserService extends IService {
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
List findByItem(String name, LocalDate start, LocalDate end);
/**
* 根据姓名分页查询
* @param pageNo
* @param pageSize
* @param name
* @return
*/
IPage pageByItem(int pageNo,int pageSize,String name);
}
package com.project.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.UserBean;
import com.project.dao.UserDao;
import com.project.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
* 自定义接口实现类
* 继承ServiceImpl
* 注意:实现类必须继承mybatis-plus提供的ServiceImpl实现类,
* 并加入泛型<持久层接口,实体bean>
*/
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl implements UserService {
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
@Override
public List findByItem(String name, LocalDate start, LocalDate end) {
// //方案1,不写xml
QueryWrapper wrapper = new QueryWrapper();
if (name!=null&&!name.equals("")){
wrapper.like("f_userName",name);
}if (start!=null){
wrapper.ge("userDate",start);
}if (end!=null){
wrapper.le("userDate",end);
}
List list = this.list(wrapper);
return list;
//方案2 书写持久层接口和xml文件
//通过this.baseMapper
List list = this.baseMapper.findByItem(name, start, end);
return list;
}
/**
* 根据姓名分页查询
* @param pageNo
* @param pageSize
* @param name
* @return
*/
@Override
public IPage pageByItem(int pageNo, int pageSize, String name) {
// Page page = new Page<>(pageNo,pageSize);
// QueryWrapper wrapper = new QueryWrapper();
// if (name!=null&&!name.equals("")){
// wrapper.like("f_userName",name);
// }
//
// IPage p = this.page(page,wrapper);
// return p;
}
}
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.MainServer;
import com.project.bean.UserBean;
import com.project.dao.UserDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainServer.class)
public class TestUserDao {
@Resource
private UserDao userDao;
@Test
public void test(){
//添加用户
UserBean userBean = new UserBean("孙悟空", LocalDate.parse("1299-09-24"),"男","123456","用户");
userDao.insert(userBean);
//根据id查询用户信息
userDao.selectById(2);
//根据用户姓名和用户密码查询用户
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq("f_userName","周公");
wrapper.eq("f_userPwd","123");
List list = userDao.selectList(wrapper);
for (UserBean i:list){
System.out.println(i);
}
//根据用户id修改用户
// UserBean userBean2 = new UserBean();
userBean2.setUserId(3);
userBean2.setUserName("周瑜好帅哦");
userBean2.setUserPwd("666");
userDao.updateById(userBean2);
//根据用户的姓名修改用户
userBean2.setUserType("游客");
userDao.update(userBean2,wrapper.eq("f_userName","孙悟空"));
//根据用户id删除用户
userDao.deleteById(4);
//根据用户身份删除用户
userDao.delete(wrapper.eq("f_userType","游客"));
//查询所有用户集合
List list = userDao.selectList(null);
for (UserBean i:list){
System.out.println(i);
}
//根据姓名和性别查询用户集合
wrapper.eq("f_userName","李逵");
wrapper.eq("f_userGender","男");
List list = userDao.selectList(wrapper);
for (UserBean i:list){
System.out.println(i);
}
//分页
Page page = new Page<>(1,3);
wrapper.like("f_userName","好");
wrapper.eq("f_userGender","男");
wrapper.eq("f_userType","用户");
IPage p = userDao.selectPage(page,wrapper);
//获取总记录数
long total = p.getTotal();
System.out.println(total);
//当前页码
long num = p.getCurrent();
System.out.println(num);
//获取当前页数据
List list = p.getRecords();
System.out.println(list);
for (UserBean i:list){
System.out.println(i);
}
//模糊查询,以生日范围查询用户信息
//比较范围用ge()--大于等于和le()--小于等于
// gt()--大于和lt()--小于
wrapper.gt("f_userDate","2022-09-01");//大于
wrapper.lt("f_userDate","2022-09-15");
List list = userDao.selectList(wrapper);
for (UserBean i:list){
System.out.println(i);
// }
}
}
package com.project;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.project.dao")
public class MainServer {
public static void main(String[] args) {
SpringApplication.run(MainServer.class,args);
}
}