全部项目结构
1:实体
@Entity//标明这是实体类并且向IOC容器注入Bean)也可以指定其name属性对应数据库表@Data//添加get,set,tostring注解) public class Goods {
@Id//(标明这是主键)
private int id;
@Column//实体类属性名如果已数据库字段名不一样就用(使用name属性=“数据库字段名”())
private String name;
private int price;
private int typed;
//一对多的字表实体类名称
@ManyToOne(targetEntity = typedea.class)
//name对应是主表关联字表的外键,referencedColumnName(字表的主键或者是与主表对应的字段)
@JoinColumn(name = "typed",referencedColumnName = "id",insertable = false,updatable = false)
private typedea type;
2:Mapper层
//Modifying 使用查询不用加,增删改要加,不然SQL语句执行不了
@Modifying
//乐观锁:将数据库price状态改为1,标识已经有用户登录
@Query(value = "update user set price=1 where id=1 ",nativeQuery = true)
Integer findUpdate();
//乐观锁:将数据库price状态改为0,标识没有用户登录
@Modifying
@Query(value = "update user set price=0 where id=1 ",nativeQuery = true)
Integer findUpdate2();
//查询当前状态
@Query(value = "select price from user where id=1",nativeQuery = true)
Integer findSelect();
3:Controller层
//判断是否可以登录数据库price字段为0就是可以登录
if (update==0) {
System.out.println(000);
//将price字段该成1标识已经登录
Integer update1 = iCustomerBiz.findUpdate();
//存储当前可以登录的浏览器的SessionID(下次在同一个浏览器登录的话就不根据数据库price字段判断登录而是根据登录过得浏览器SessionID登录
// model.addAttribute("sessionId3",sessionId);
// session.setAttribute("update", update);
//登录成功的页面
return "lol";
//如何update11和update2都不等于null并且SessionID又不一样就说明不是一个浏览器了
}else if (update11!=null&&!update2.equals(update11)){
System.out.println(update11);
System.out.println(update2);
System.out.println();
//失败的页面
return "kk";
}
2:springboot整合mybatis-plus做curd加分页插件
1:Config层
@EnableTransactionManagement @MapperScan(value = "com.springplus.springbootmybatisplus.Mapper") @Configuration//标明这是配置类 public class MybatisPlusConfig { //分页插件 @Bean//MybatisPlusInterceptor PaginationInnerInterceptor public MybatisPlusInterceptor paginationInnerInterceptor () { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); return interceptor; }
2:mapper自定义多表分页
public interface Conformity extends BaseMapper<Goods> { //CourseInfoList StudentAxndMajorVO Constants IPage<Combination> selectStudentAndMajorXmlPage(Page<Combination> page, @Param(Constants.WRAPPER) Wrapper<Combination> wrapper); }
3:Controller层
package com.springplus.springbootmybatisplus.Controller; 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.springplus.springbootmybatisplus.Mapper.*; import com.springplus.springbootmybatisplus.Pojo.Combination; import com.springplus.springbootmybatisplus.Pojo.Goods; import com.springplus.springbootmybatisplus.server.ConformityService; import com.springplus.springbootmybatisplus.server.GoodsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { @Autowired private ConformityService conformityService; @Autowired private Conformity conformity; @Autowired private GoodsService goodsService; @Autowired private sdasdasd sdasdasd; @RequestMapping("demo1") public List<Goods> demo1(){ //Goods goods =conformity.selectList(null); List<Goods> goods = sdasdasd.selectList(null); return goods; } @RequestMapping("demo2") public Goods demo2(){ //Goods goods =conformity.selectList(null); Goods goods1 = sdasdasd.selectById("2"); // List<Goods> goods = goods1; return goods1; } @RequestMapping("demo3") public List demo3(){ //Goods goods =conformity.selectList(null); // Goods goods1 = sdasdasd.selectById("2"); QueryWrapper wrapper=new QueryWrapper(); wrapper.eq("id", 3); List list = sdasdasd.selectList(wrapper); // List<Goods> goods = goods1; return list; } @RequestMapping("demo4") public List demo4(){ //Goods goods =conformity.selectList(null); // Goods goods1 = sdasdasd.selectById("2"); QueryWrapper wrapper=new QueryWrapper(); //like %?% likeLeft%? likeRight?% wrapper.like("name", "1"); // wrapper.eq("id", 3); List list = sdasdasd.selectList(wrapper); // List<Goods> goods = goods1; return list; } @RequestMapping("demo5") public Integer demo5(){ //Goods goods =conformity.selectList(null); // Goods goods1 = sdasdasd.selectById("2"); // QueryWrapper wrapper=new QueryWrapper(); // //like %?% likeLeft%? likeRight?% // wrapper.like("name", "1"); // wrapper.eq("id", 3); Goods goods=new Goods(); goods.setId(11); goods.setName("张三"); goods.setPrice(0); goods.setTyped(2); int insert = sdasdasd.insert(goods); //List list = sdasdasd.selectList(wrapper); // List<Goods> goods = goods1; return insert; } @RequestMapping("demo6") public int demo6(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.eq("price", "0"); Goods goods=new Goods(); goods.setId(1); int update = sdasdasd.update(goods, queryWrapper); int i = sdasdasd.updateById(goods); return update; } @RequestMapping("demo7") public int demo7(){ QueryWrapper queryWrapper=new QueryWrapper(); Goods goods = sdasdasd.selectById(1); goods.setName("王五"); int i = sdasdasd.updateById(goods); return i; } //删除 @RequestMapping("demo8") public int demo8(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.ge("price", 10); int delete = sdasdasd.deleteById("6"); return delete; } @RequestMapping("demo9") public IPage demo9(){ QueryWrapper queryWrapper=new QueryWrapper(); queryWrapper.like("name", "1"); IPage<Goods> page=new Page(1,1); IPage page1=sdasdasd.selectPage(page, queryWrapper); return page1; } @RequestMapping("demo11") public Goods selectStudentAndMajorXmlPage(){ // List<Combination> studentAndMajorVOS = conformityService.selectStudentAndMajorXmlPage(1, 4); Goods goods = goodsService.selectByPrimaryKey(2); return goods; } }
3:所有依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springplus</groupId> <artifactId>springboot-mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-mybatisplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--引入MybatisPlus核心配置文件--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!--代码生成器--> <!--当前包未传递依赖 MP 包,需要自己引入!--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> <!-- 模板生成器引擎配合代码生成器一起使用 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.springplus.springbootmybatisplus.SpringbootMybatisplusApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>