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

天津网站制作公司哪家好帝国cms 商城网站视频教程

天津网站制作公司哪家好,帝国cms 商城网站视频教程,phpcms做网站感想,常见的推广平台有哪些文章目录 前言一、准备1. 版本要求2.安装3. 建表语句 二、案例1. mapper2.实体类3.测试类4.扫描5. 配置6. mapper.xml7.输出 总结 前言 MyBatis-Spring-Boot-Starter 可以帮助你更快地在 Spring Boot 之上构建 MyBatis 应用。 一、准备 1. 版本要求 MyBatis-Spring-Boot-Sta…

文章目录

  • 前言
  • 一、准备
    • 1. 版本要求
    • 2.安装
    • 3. 建表语句
  • 二、案例
    • 1. mapper
    • 2.实体类
    • 3.测试类
    • 4.扫描
    • 5. 配置
    • 6. mapper.xml
    • 7.输出
  • 总结


前言

MyBatis-Spring-Boot-Starter 可以帮助你更快地在 Spring Boot 之上构建 MyBatis 应用。


一、准备

1. 版本要求

MyBatis-Spring-Boot-StarterMyBatis-SpringSpring BootJava
3.03.03.0 - 3.117 或更高
2.32.12.5 - 2.78 或更高

2.安装

<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>

3. 建表语句

CREATE TABLE user  (`id` int NOT NULL,`name` varchar(255) NULL,`age` int NULL,`brith_day` date NULL,PRIMARY KEY (`id`)
);
INSERT INTO `springboot`.`user`(`id`, `name`, `age`, `brith_day`) VALUES (1, '张三', 11, '2014-05-19');
INSERT INTO `springboot`.`user`(`id`, `name`, `age`, `brith_day`) VALUES (2, '李四', 10, '2015-05-19');

二、案例

正如你已经知道的, 要与 Spring 一起使用 MyBatis,你至少需要一个 SqlSessionFactory 和一个 mapper 接口。
MyBatis-Spring-Boot-Starter 将会:

  • 自动探测存在的 DataSource
  • 将使用 SqlSessionFactoryBean 创建并注册一个 SqlSessionFactory 的实例,并将探测到的 DataSource 作为数据源
  • 将创建并注册一个从 SqlSessionFactory 中得到的 SqlSessionTemplate 的实例
  • 自动扫描你的 mapper,将它们与 SqlSessionTemplate 相关联,并将它们注册到Spring 的环境(context)中去,这样它们就可以被注入到你的 bean 中

1. mapper

package org.example.springboot3.mybatis.mappers;import org.apache.ibatis.annotations.Mapper;
import org.example.springboot3.mybatis.model.User;import java.util.List;/*** Create by zjg on 2024/5/19*/
@Mapper
public interface UserMapper {List<User> selectList();
}

2.实体类

package org.example.springboot3.mybatis.model;import lombok.Getter;
import lombok.Setter;
import lombok.ToString;import java.util.Date;/*** Create by zjg on 2024/5/19*/
@Getter
@Setter
@ToString
public class User {private int id;private String name;private int age;private Date brithDay;
}

3.测试类

package org.example.springboot3.mybatis.controller;import lombok.extern.log4j.Log4j2;
import org.example.springboot3.mybatis.mappers.UserMapper;
import org.example.springboot3.mybatis.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;/*** Create by zjg on 2024/5/19*/
@RequestMapping("/mybatis/")
@RestController
@Log4j2
public class UserController {@AutowiredUserMapper userMapper;@RequestMapping("001")public List mybatis001(){List<User> users = userMapper.selectList();log.info(users);return users;}
}

4.扫描

package org.example.springboot3;import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@MapperScan("org.example.springboot3.mybatis.mappers")
@RestController
@SpringBootApplication
public class SpringBoot3Application {private static final Logger log = LoggerFactory.getLogger(SpringBoot3Application.class);public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(SpringBoot3Application.class, args);String appName = run.getEnvironment().getProperty("spring.application.name");log.info("{}启动完成",appName);}@RequestMapping("/")String home() {return "Hello SpringBoot!";}
}

5. 配置

#mybatis
mybatis:mapper-locations: mappers/**/*.xmltype-aliases-package: org.example.springboot3.mybatis.modeltype-handlers-package: org.example.springboot3.mybatis.typehandlerconfiguration:map-underscore-to-camel-case: truedefault-fetch-size: 100default-statement-timeout: 30

更多配置请查看mybatis配置参数

6. mapper.xml

<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.example.springboot3.mybatis.mappers.UserMapper"><select id="selectList" resultType="user">select * from user</select>
</mapper>

7.输出

[2024-05-19 16:40:38.402][http-nio-8080-exec-1][INFO]- org.example.springboot3.mybatis.controller.UserController.mybatis001(UserController.java:23) - [User(id=1, name=张三, age=11, brithDay=Mon May 19 00:00:00 CST 2014), User(id=2, name=李四, age=10, brithDay=Tue May 19 00:00:00 CST 2015)]

总结

回到顶部

架子这就搭好喽,比上一章顺利多了。

更多内容请查看《Mybatis》系列文章目录

http://www.yayakq.cn/news/225385/

相关文章:

  • 网站建设5000费用预算班级网页设计图片
  • 河北建设执业信息网站wordpress 社区主题
  • 杭州企业网站设计模板学历提升朋友圈文案
  • 网站建设费用能否计入开办费做简图的网站
  • 深圳网站设计师wordpress 无法上传文件
  • 百度站长工具如何使用wordpress 自定义小工具
  • 上海知名网站推广天津武清做淘宝网站
  • 鞍山网站制作小程序网站建设公司找哪里
  • 网站建设中的html页面彩票网站开发需要多少钱
  • 嘉定建设厅网站网站正能量大全
  • 深圳网站设计机构wordpress4.9.8主题
  • 北京网站建设公司黄页vi设计公司深圳
  • wordpress 媒体库 插件百度seo不正当竞争秒收
  • 网站建设中英语做网站什么码
  • 手机移动开发网站建设品牌营销策划有限公司
  • 中国协会网站建设方案免费做明信片的网站
  • 邢台精品网站建设安徽省工程建设信息网职称查询
  • 企业网站的建设目的有什么惠州市建设局网站
  • 页面简洁的网站网站设计师待遇
  • html5 网站建设网站开发费用会计分录
  • 广州网站推广自助正规企业查询
  • 公司网站开发维护如何自己做网站优化
  • 服务公司网站建设wordpress主题结构图
  • 沧县住房和城乡建设局网站上海外包公司排行
  • 成都犀牛网站建设公司聚美优品网站建设的目标
  • 无需下载的网站vi设计公司 成都
  • 西樵网站建设建英语网站首页
  • 龙岩网站制作公司引擎搜索优化
  • dede 网站源码网络规划设计师教程第2版pdf
  • 金华专业的网站建设网站服务器位于北美