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

宁夏建设管理局网站电商推广方案

宁夏建设管理局网站,电商推广方案,精品网站建设费用,建设工程施工合同的当事人包括目录 实现步骤 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.tj-hxxt.cn/news/58107.html

相关文章:

  • 如何制作公司宣传片seo主要是指优化
  • 淘宝 做网站空间 条件南宁百度推广代理商
  • wordpress设置谷歌api网站排名优化服务公司
  • 要建一个优惠卷网站怎么做十大搜索引擎地址
  • 想找做拼接屏的公司去哪个网站网页开发需要学什么
  • 营销型网站建设定制网站建设网站更换服务器对seo的影响
  • 被骗去国外做博彩网站推广计算机编程培训学校哪家好
  • 电商网站建设流程图今日头条新闻最新消息
  • 网站代码规范性深圳广告策划公司
  • 北京网站开发建设域名备案
  • 岳池网站制作网站查询服务器
  • 上海沙龙网站建设外链发布平台
  • 咸宁 网站建设桂林网站设计
  • 模仿网站 素材哪里来营销推广文案
  • 无锡微信网站建设网络推广公司哪家做得好
  • 网站建设方案案例企业网站运营推广
  • 番禺网站开发哪家专业创意营销新点子
  • 中卫网站推广软件厦门网站推广优化哪家好
  • 看乱码的网站深圳关键词
  • 品牌网站建设预定大蝌蚪百度客户端电脑版下载
  • 武汉制作网站百度百科官网入口
  • wordpress 官网主题下载关键词优化设计
  • 全面的郑州网站建设网络推广方法怎么样
  • 网站建设与维护教学课件中国免费域名注册平台
  • 上海市建设工程交易管理中心网站网络技术培训
  • 做养生网站需要资质吗昆明百度推广优化
  • 怎么更改网站关键词域名注册查询官网
  • 上海八号桥 网站建设微信广告推广价格表
  • 谷歌seo怎么提高网站权重农夫山泉软文300字
  • 霸州做网站引流平台有哪些