您的位置:首页 > 汽车 > 新车 > 建站平台备案_建筑公司使命愿景价值观_seo是什么工作内容_成都自然排名优化

建站平台备案_建筑公司使命愿景价值观_seo是什么工作内容_成都自然排名优化

2024/10/5 21:28:19 来源:https://blog.csdn.net/m0_58970439/article/details/142652241  浏览:    关键词:建站平台备案_建筑公司使命愿景价值观_seo是什么工作内容_成都自然排名优化
建站平台备案_建筑公司使命愿景价值观_seo是什么工作内容_成都自然排名优化

1. MybatisPlus

1.1 ORM介绍

        ORM(Object Relational Mapping,对象关系映射)是为了解决面向对象与关系数据库存在的互不匹配现象的一种技术。

        比如,将java中的对象传递到关系型数据库中去,或者将关系型数据库传递到java大对象中去,这样一个映射关系,需要一个中间组件的接入。这个映射既包含了存储,又包含了读取。

        ORM通过使用描述对象和数据库之间映射的元数据将程序中的对象自动持久化到关系数据库中。

        ORM框架的本质是简化编程中操作数据库的编码。

1.2 MyBatis-Plus介绍 及配置

        MyBatis是一款优秀的数据持久层ORM框架,被广泛地应用于应用系统。

        MyBatis能够非常灵活地实现动态SQL,可以使用XML或注解来配置和映射原生信息,能够轻松地将Java的POJO(Plain Ordinary Java Object,普通的Java对象)与数据库中的表和字段进行映射关联。

        MyBatis-Plus是一个 MyBatis 的增强工具,在 MyBatis 的基础上做了增强,简化了开发。

1.2.1添加依赖

注:MyBatis-Plus配置版本问题很恶心

在pom.xml文件中配置dependency

        <!--MyBatisPlus依赖--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><!--mysq1驱动依赖 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!--数据连接池 druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.20</version></dependency>
1.2.2配置数据库相关信息

在application.properties配置

spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mapper?useSSL=false&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
1.2.3添加@MapperScan注解

在org/example/mp_demo/MpDemoApplication.java中添加注解

package org.example.mp_demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("org.example.mp_demo.mapper")//扫描器,告诉他扫描哪个包
public class MpDemoApplication {public static void main(String[] args) {SpringApplication.run(MpDemoApplication.class, args);}}

1.3 Mybatis CRUD注解

如下所示:

conroller为所编写的控制器,User是class,UserMapper是接口

1.3.1 控制器
@RestController
public class UserController {@Autowired//注入userMapper  但是直接声明会导致其为空,因此加上注解@Autowired让其不为空private UserMapper userMapper;//如果要用Mapper,提前声明一下就好了,Mybatis会帮我们自动配置↑↑↑@GetMapping("/user")public List query(){List<User> list = userMapper.find();System.out.println(list);return list;}}
1.3.2 User类
public class User {private int id;private int name;private int password;private int birthday;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getName() {return name;}public void setName(int name) {this.name = name;}public int getPassword() {return password;}public void setPassword(int password) {this.password = password;}public int getBirthday() {return birthday;}public void setBirthday(int birthday) {this.birthday = birthday;}@Overridepublic String toString() {return "User{" +"id=" + id +", name=" + name +", password=" + password +", birthday=" + birthday +'}';}
}

mysql数据库mapper中的user:

运行得到:(网页自动转化成json格式)

 

1.3.3 UserMapper接口
@Mapper //声明这是一个Mapper组件,会让MapperScan扫描mapper包,找到这个Mapper组件
public interface UserMapper  {//查询所有用户@Select("select * from user")//这个sql语句对自动去找配置文件application.properties中的mapper数据库//会自动发select到的数据放到List里面去。public List<User> find();@Insert("insert into user values (#{id},#{name},#{password},#{birthday})")public int insert(User user);//返回值代表插入了几条记录,如果插入失败则返回0}
1.3.4 CRUD操作

 1.4 MyBatis-Plus

当我们选用了MyBatis-Plus后,以上编写的均可以不写。

        接口UserMapper改动:继承BaseMapper,MyBatis-Plus可以自动根据这个类User找到user表,帮我们做增删改查。

@Mapper //声明这是一个Mapper组件,会让MapperScan扫描mapper包,找到这个Mapper组件
public interface UserMapper extends BaseMapper<User> {//MyBatis-Plus可以自动根据这个类User找到user表,帮我们做增删改查。//    //查询所有用户
//    @Select("select * from user")
//    //这个sql语句对自动去找配置文件application.properties中的mapper数据库
//    //会自动发select到的数据放到List里面去。
//    public List<User> find();
//
//    @Insert("insert into user values (#{id},#{name},#{password},#{birthday})")
//    public int insert(User user);//返回值代表插入了几条记录,如果插入失败则返回0
}

         控制器改动:

@RestController
public class UserController {@Autowired//注入userMapper  但是直接声明会导致其为空,因此加上注解@Autowired让其不为空private UserMapper userMapper;//如果要用Mapper,提前声明一下就好了,Mybatis会帮我们自动配置↑↑↑@GetMapping("/user")public List query(){List<User> list = userMapper.selectList(null);//selectList()该函数查询,括号内是查询条件,没有就填nullSystem.out.println(list);return list;}@PostMapping("/user")public String save(User user){int i = userMapper.insert(user);if(i > 0){return "插入成功";}else {return "插入失败";}}
}
1.4.1 表名类名不一致

        如果数据库数据表名字和class类名不一致该怎么办?

@TableName("t_user")
public class User {private int id;private int name;private int password;private int birthday;
1.4.2 表名类名属性不一样

         如果数据库数据表属性名字和class类里的属性不一致该怎么办?

public class User {private int id;@TableField("nickname")private int name;private int password;private int birthday;
1.4.3 类里的名称数据库不存在
    private int id;@TableField("nickname")private int name;private int password;private int birthday;@TableField(exist = false)private int age;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com