当前位置: 首页 > news >正文

新网站建设方案手机百度2022年新版本下载

新网站建设方案,手机百度2022年新版本下载,建设营销网站时以什么为导向,不懂代码做网站目录 实现步骤 1. 在项目的 pom.xml 配置文件中引入如下依赖 2. 在项目的 application.properties 配置文件中添加如下依赖 3. 新建 UserMapper.class 接口类,添加如下 3 个方法 4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xm…

目录

实现步骤

1. 在项目的 pom.xml 配置文件中引入如下依赖

2. 在项目的 application.properties 配置文件中添加如下依赖

3. 新建 UserMapper.class 接口类,添加如下 3 个方法

4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xml 文件,添加如下操作数据库配置

5. 省略 UserService.class/UserServiceImpl.class 类代码,创建 UserController.class 并添加如下代码测试

补充


实现步骤

1. 在项目的 pom.xml 配置文件中引入如下依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</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>
</dependency>

注意:这里我们没有引入其他的数据源,所以引入 spring-boot-starter-data-jdbc 依赖,使用 Hikari 数据源,如果要使用其他的数据源,则需要引入对应依赖即可

2. 在项目的 application.properties 配置文件中添加如下依赖

#################### 数据库连接池配置 ####################
# 数据库连接地址
spring.datasource.url = jdbc:mysql://localhost:3306/spring_study?characterEncoding=utf8&serverTimezone=UTC
# 数据库驱动类
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# 数据库用户名
spring.datasource.username = root
# 数据库密码
spring.datasource.password = dufu9137*
# 最小空闲连接
spring.datasource.hikari.minimum-idle = 2
# 最大连接数
spring.datasource.hikari.maximum-pool-size = 3
# 连接最大存活时间(应大于等于 30000)
spring.datasource.hikari.max-lifetime = 30000
# 空闲连接超时时间(应小于 max-lifetime 的值)
spring.datasource.hikari.idle-timeout = 20000
# 用于测试连接是否可用的查询语句
spring.datasource.hikari.connection-test-query = SELECT 1#################### MyBatis 配置 ####################
# 指定 Mapper.xml 文件的位置
mybatis.mapper-locations = classpath:mybatis/mapper/*.xml
# 开启驼峰命名映射规则
mybatis.configuration.map-underscore-to-camel-case = true
# 打开日志输出功能
mybatis.configuration.log-impl = org.apache.ibatis.logging.stdout.StdOutImpl
# 设置实体类映射别名所在包路径
mybatis.type-aliases-package = com.study.springboot.entities
# 设置操作超时时间
mybatis.configuration.default-statement-timeout = 3
# 开启缓存
mybatis.configuration.cache-enabled = true

3. 新建 UserMapper.class 接口类,添加如下 3 个方法

@Mapper
public interface UserMapper {User getUserById(Integer id);List<User> getUserList();Integer addUser(User user);
}

4. 在 /resources/mybatis/mapper 路径(需要手动创建文件夹)下创建 UserMapper.xml 文件,添加如下操作数据库配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.springboot.mapper.UserMapper"><select id="getUserById" parameterType="int" resultType="User">select * from user where id = #{id}</select><select id="getUserList" resultType="User">select * from user order by id asc</select><insert id="addUser" parameterType="User" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into user(name) values(#{name})</insert>
</mapper>

5. 省略 UserService.class/UserServiceImpl.class 类代码,创建 UserController.class 并添加如下代码测试

@RestController
public class UserController {@Resourceprivate UserService userService;@GetMapping("/byId")public User getUserById(@RequestParam Integer id) {return userService.getUserById(id);}@GetMapping("/list")public List<User> getUserList() {return userService.getUserList();}@PostMapping("/add")public User addUser(@RequestBody User user) {Integer count = userService.addUser(user);return count == 1 ? user : null;}
}

补充

1. xxxMapper.class 上添加 @Mapper 注解表明这是一个 Mapper 类,也可以在项目启动类或添加了 @Configuration 注解的配置类上添加 @MapperScan("com.study.springboot.mapper") 注解表明这个包路径下的所有接口类都是 Mapper 类;但建议使用 @Mapper 注解实现

2. 我们这里使用的 xxxMapper.xml 配置文件的方式添加 SQL 语句操作数据库,也可以在 xxxMapper.class 类上直接使用 @Select、@Update、@Insert、@Delete 注解直接添加 SQL 语句操作;注意注解式添加 SQL,方法中的参数可能需要 @Param 注解标记

3. 对于自增的记录,有时候我们需要插入操作后直接返回插入的结果,但是得不到自增的主键,如果是使用 xxxMapper.xml 文件方式,可以通过添加如下属性解决这个问题

<insert id="addUser" parameterType="User" useGeneratedKeys="true" keyColumn="id" keyProperty="id">insert into user(name) values(#{name})
</insert>

如果是使用注解的方式,那么可通过添加 @Options 注解添加属性解决这个问题

@Insert("insert into user(name) values(#{name})")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
Integer addUser(User user);

useGeneratedKeys = true,表示使用自增主键

keyColumn = "id",数据库中主键列名

keyProperty = "id",映射实体类主键字段

4. MyBatis 操作数据库时,传递的参数为对象的话,在 SQL 语句中,直接使用对象的属性占位即可,例如

@Insert("insert into user(name) values(#{name})")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
Integer addUser(User user);

如果参数使用了 @Param 注解的话,则需要使用 @Param 注解的值.对象属性占位,例如

@Insert("insert into user(name) values(#{user.name})")
@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
Integer addUser(@Param("user") User user);

http://www.hkea.cn/news/695636/

相关文章:

  • 春节网站怎么做小说排行榜百度搜索风云榜
  • 商城服务是什么软件seo是指什么岗位
  • 无锡网站建设有限公司网站快速收录的方法
  • 网站建设通报推广网站多少钱
  • 网络推广公司成都seo排名优化教程
  • 一台手机登录微信网页版西安优化外
  • 如何做旅游攻略网站长沙seo优化推荐
  • 长春火车站电话咨询电话快排seo
  • 龙城建设网站公司网站内容优化方法
  • 南通网站建设搭建网站卖链接
  • 驻马店市做网站seo臻系统
  • 找公司做网站怎么图片都要自己找百度推广官网电话
  • 网站小样用什么做seo外链平台热狗
  • 建站点的步骤sem是什么
  • 深圳专业做网站的衡水网站优化推广
  • 徐汇科技网站建设2345中国最好的网址站
  • 邢台论坛吧百度seo收录软件
  • 做国外服务器网站吗怎么让百度搜索靠前
  • 做动态图网站有哪些自建站怎么推广
  • web网站开发课程设计报告seo技术培训沈阳
  • 会宁网站建设公司网站优化助手
  • 网站设计制作体会2023年5月最新疫情
  • 月亮湾设计有限公司网站南宁seo产品优化服务
  • 福田欧曼服务站电话上海高端seo公司
  • 高端网站建设哪家好谷歌seo和百度seo
  • 前端写一个页面多少钱海口网站关键词优化
  • 浦东新区建设局官方网站东莞seo关键词
  • 在百度做橱柜网站进入百度一下官网
  • wordpress调用分类标签站长工具查询seo
  • 网站做全局搜索云南新闻最新消息今天