1.建立项目
????????file->New->Project
?????????选择Maven项目
?
?
2.导入依赖
????????编写pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--web开发依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<!--数据库驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!--阿里巴巴连接池依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<!--测试框架依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Springboot测试框架依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--JWT依赖-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.13.0</version>
</dependency>
3.书写配置文件,连接数据库
? ? ? ? 编写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 #对应项目结构
? ? ? ? 数据库设计
?
4.构建项目
????????建立包com.project,在此包下建立bean、dao、service包,分别建立用户实体bean,持久层,业务层等,以及在resources目录下下创建映射文件 持久层名.xml
????????创建结束后的结构
?
4.1.各个包下的实现代码块(按自己数据库设计需求来编写,此处为本人数据库实列)
? ? ? ? bean包下用户实体类UserBean:
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 + '\'' +
'}';
}
}
? ? ? ? dao层(持久层)UserDao类:
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<UserBean> {
//此时持久接口中,已经包含基本单表的增删查改的实现
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
List<UserBean> findByItem(@Param("name")String name,
@Param("start")LocalDate start,
@Param("end") LocalDate end);
/**
* 根据姓名分页查询
* @param page
* @param name
* @return
*/
IPage<UserBean> pageByItem(Page<UserBean> page,
@Param("name")String name);
}
????????
? ? ? ? service层(业务层)UserService类:
????????
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<UserBean> {
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
List<UserBean> findByItem(String name, LocalDate start, LocalDate end);
/**
* 根据姓名分页查询
* @param pageNo
* @param pageSize
* @param name
* @return
*/
IPage<UserBean> pageByItem(int pageNo,int pageSize,String name);
}
????????service层(业务层)下impl包UserServiceImpl类:
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<UserDao, UserBean> implements UserService {
/**
* 根据用户姓名、生日查询用户信息
* @param name
* @param start
* @param end
* @return
*/
@Override
public List<UserBean> findByItem(String name, LocalDate start, LocalDate end) {
// //方案1,不写xml
QueryWrapper<UserBean> 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<UserBean> list = this.list(wrapper);
return list;
//方案2 书写持久层接口和xml文件
//通过this.baseMapper
List<UserBean> list = this.baseMapper.findByItem(name, start, end);
return list;
}
/**
* 根据姓名分页查询
* @param pageNo
* @param pageSize
* @param name
* @return
*/
@Override
public IPage<UserBean> pageByItem(int pageNo, int pageSize, String name) {
// Page<UserBean> page = new Page<>(pageNo,pageSize);
// QueryWrapper wrapper = new QueryWrapper();
// if (name!=null&&!name.equals("")){
// wrapper.like("f_userName",name);
// }
//
// IPage<UserBean> p = this.page(page,wrapper);
// return p;
}
}
4.2.在test/java目录下新建TestUserDao测试类,测试持久层代码
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<UserBean> wrapper = new QueryWrapper<>();
wrapper.eq("f_userName","周公");
wrapper.eq("f_userPwd","123");
List<UserBean> 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<UserBean> list = userDao.selectList(null);
for (UserBean i:list){
System.out.println(i);
}
//根据姓名和性别查询用户集合
wrapper.eq("f_userName","李逵");
wrapper.eq("f_userGender","男");
List<UserBean> list = userDao.selectList(wrapper);
for (UserBean i:list){
System.out.println(i);
}
//分页
Page<UserBean> page = new Page<>(1,3);
wrapper.like("f_userName","好");
wrapper.eq("f_userGender","男");
wrapper.eq("f_userType","用户");
IPage<UserBean> p = userDao.selectPage(page,wrapper);
//获取总记录数
long total = p.getTotal();
System.out.println(total);
//当前页码
long num = p.getCurrent();
System.out.println(num);
//获取当前页数据
List<UserBean> 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<UserBean> list = userDao.selectList(wrapper);
for (UserBean i:list){
System.out.println(i);
// }
}
}
5.编写MainServer准备测试
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);
}
}
|