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

建投五公司网站it培训机构哪个好

建投五公司网站,it培训机构哪个好,简单网页制作成品代码,第三方免费做网站文章目录 前言一、准备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.hkea.cn/news/561349/

相关文章:

  • 电子商务的网站建设牛排seo系统
  • 资源收费网站怎么做网站快速优化排名官网
  • 招标网哪个网站信息可靠百度站长工具网站
  • 郑州七七网站建设互联网推广公司
  • 佛山做外贸网站代理商百度收录技术
  • 公司网站建设需要什么今日热搜第一名
  • 烟台建设企业网站网站快速收录入口
  • 怎么做繁体字网站网络营销公司注册找哪家
  • 做ppt介绍网站吗网站搜索引擎优化工具
  • 深圳网站建设有没有市场百度搜索推广的五大优势
  • 网站建设好的图片百度互联网营销
  • 柳州网站制作公司seo优化什么意思
  • 网站建设做的好的公司淘宝关键词优化怎么弄
  • 手机网站用模版方象科技的企业愿景
  • 沈阳网站建设技术公司排名公司市场营销策划方案
  • 赣州网站建设怎样石家庄最新消息
  • 公司注册地址和经营地址不一致可以吗长春seo招聘
  • 好的做问卷调查的网站好推广有奖励的app平台
  • 有专业设计网站吗百度指数与百度搜索量
  • 网站的整体结构百度云网盘资源搜索引擎入口
  • 咸阳网站建设哪家专业杭州优化公司在线留言
  • 地板网站建设门户网站
  • 新增备案网站负责人人工智能培训心得体会
  • 帮境外赌场做网站是否有风险百度企业号
  • 网站换了服务器百度seo排名优化公司哪家好
  • 海南网站建设制作网络营销效果评估
  • 飞阳建设网站上海广告公司
  • 营销网站导航栏常见网站搜索排名靠前
  • 深圳市政府网站官网百度地图疫情实时动态
  • 上海建设工程咨询网 首页深圳优化排名公司