大连h5建站网站优化的方法
1、MyBatis-Plus概述
MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
【技术储备】
拥有 Java 开发环境以及相应 IDE
熟悉 Spring Boot
熟悉 Maven
【特征】
为简化开发而生
1、只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑。
2、效率至上:只需简单配置,即可快速进行单表 CRUD 操作,从而节省大量时间。
3、丰富功能:代码生成、自动分页、逻辑删除、自动填充等功能一应俱全。
2、数据库支持
任何能使用 MyBatis
进行 CRUD, 并且支持标准 SQL 的数据库,具体支持情况如下,如果不在下列表查看分页部分教程 PR 您的支持。
MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb,informix,TDengine,redshift
达梦数据库,虚谷数据库,人大金仓数据库,南大通用(华库)数据库,南大通用数据库,神通数据库,瀚高数据库,优炫数据库,星瑞格数据库
3、快速上手案例
版本信息:springboot2.7.14+mysql5.1.42+mybatis-plus3.5.3.2
【工程结构】
3.1、创建springboot工程被导入相关依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3.2</version>
</dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.42</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>
3.2、配置数据库相关信息
log-impl:控制台打印出mybatis执行时的具体sql、查询条件、返回值等
map-underscore-to-camel-case:mybatisplus在查询数据库的时候回默认的开启数据库下划线驼峰命名转化,我们需要关闭。
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisplususername: rootpassword: 123456mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: false# 如果是放在src/main/java目录下 classpath:/com/*/*/mapper/*Mapper.xml# 如果是放在resource目录 classpath:/mapper/**.xml#mapper-locations: classpath:/mapper/**.xml
3.3、创建数据库和表信息
CREATE DATABASE mybatisplus;
USE mybatisplus;
CREATE TABLE student(stu_id VARCHAR(50),stu_name VARCHAR(30),stu_sex VARCHAR(2),stu_age VARCHAR(4),stu_addr VARCHAR(50),stu_pwd VARCHAR(50)
)DEFAULT CHARSET=utf8;
INSERT INTO student VALUES('1001','晓春','男','33','安徽合肥','1001');
INSERT INTO student VALUES('1002','陈平安','男','18','安徽合肥','1002');
3.4、创建bean对象
@TableName("student"):定义映射表信息
@TableId("stu_id"):定义数据库主键
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("student")
public class Student {@TableId("stu_id")private String stu_id;private String stu_name;private String stu_sex;private String stu_age;private String stu_addr;private String stu_pwd;
}
3.5、创建Mapper接口
public interface StudentMapper extends BaseMapper<Student> {
}
3.6、在启动类中扫描mapper接口
通过@MapperScan("com.txc.mybatisplus.mapper")注解扫描mapper接口,注意地址不能写错了。
@SpringBootApplication
@MapperScan("com.txc.mybatisplus.mapper")
public class Mybatisplusdemo1Application {public static void main(String[] args) {SpringApplication.run(Mybatisplusdemo1Application.class, args);}
}
3.7、功能1:查询所有学生信息
通过springweb创建一个测试类,查询student表中的所有学生信息
@RestController
public class StudentController {@Autowired(required = false)StudentMapper studentMapper;@RequestMapping("/testmybatisplus")@ResponseBodypublic void testMybatisPlus() {List<Student> list=studentMapper.selectList(null);//Assert.isTrue(5 == list.size(), "");list.forEach(System.out::println);}
}
3.8、功能2:模糊查询学生信息
说明1:queryWrapper.eq("stu_name","陈平安");//表示根据stu_name字段查询陈平安
效果类似于:select * from student where stu_name=’陈平安’
说明2:queryWrapper.like("stu_addr","安徽合肥");//表示根据stu_addr模糊查询安徽合肥
效果类似于select * from student where stu_addr like ’%陈平安%’
两者合起来的效果是:
select * from student where stu_name=’陈平安’ and stu_addr like ’%陈平安%’;
@Controller
public class StudentController {@Autowired(required = false)StudentMapper studentMapper;//根据姓名查询学生信息@RequestMapping("/testMybatisPlusByName")@ResponseBodypublic void testMybatisPlusByName() {//QueryWrapper封装查询信息QueryWrapper<Student> queryWrapper=new QueryWrapper<>();//表示查询的时候表字段stu_name的值为陈平安queryWrapper.eq("stu_name","陈平安");//表示根据stu_addr模糊查询安徽合肥queryWrapper.like("stu_addr","安徽合肥");List<Student> list=studentMapper.selectList(queryWrapper);list.forEach(stu->{System.out.println(stu.getStu_name());});}
3.9、功能3:根据id查询学生信息
//根据id查询学生信息
@RequestMapping("/testMybatisPlusById")
@ResponseBody
public void testMybatisPlusById() {//QueryWrapper封装查询信息QueryWrapper<Student> queryWrapper=new QueryWrapper<>();//表示查询的时候表字段stu_name的值为陈平安queryWrapper.eq("stu_id","1001");Student stu=studentMapper.selectOne(queryWrapper);System.out.println(stu.toString());
}
}
3.10、功能3:添加学生信息
@Controller
public class StudentController {@Autowired(required = false)StudentMapper studentMapper;//添加学生信息@RequestMapping("/testMybatisPlusAdd")@ResponseBodypublic void testMybatisPlusAdd() {Student stu=new Student("1003","十一郎","男","34","安徽合肥","1003");studentMapper.insert(stu);System.out.println("=======数据添加成功========");}
}
3.11、功能4:修改学生信息
@Controller
public class StudentController {@Autowired(required = false)StudentMapper studentMapper;//修改学生信息@RequestMapping("/testMybatisPlusupdate")@ResponseBodypublic void testMybatisPlusupdate() {Student stu=new Student("1003","十一郎","男","34","安徽合肥","1003");studentMapper.updateById(stu);System.out.println("=======数据修改成功========");}
}
3.12、功能5:删除学生信息
@Controller
public class StudentController {@Autowired(required = false)StudentMapper studentMapper;//删除学生信息@RequestMapping("/testMybatisPlusDelete")@ResponseBodypublic void testMybatisPlusDelete() {Student stu=new Student();stu.setStu_id("1003");studentMapper.deleteById(stu);System.out.println("=======删除修改成功========");}
}
4、源码下载
源码属于vip资源,如果需要可在评论区留言,我修改成免费。
https://download.csdn.net/download/tangshiyilang/88276862