springboot 整合mybatis实现curd

springboot 整合mybatis

    • pom文件
    • mvc 架构
    • application.properties 扩展配置,druid配置类

项目地址:
https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-mybatis-05

pom文件

 <!--整合mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.3</version></dependency><!-- druid数据源 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version></dependency><!-- 官方提供 jdbc用来连接数据库 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- springboot 集成mvc --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 连接数据库的mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!--lombok 用于实体类的set/get 构造方法--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>

mvc 架构

  • 实体类
package cn.bitqian.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author echo lovely* @date 2020/10/25 10:11*/
@Data // set/get
@NoArgsConstructor // 无参
@AllArgsConstructor // 有参
public class Users {private Integer userId;private String userName;private String userPassword;}
  • 基本的curd
package cn.bitqian.mapper;import cn.bitqian.entity.Users;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;import java.util.List;/*** user mapper* @author echo lovely* @date 2020/10/25 10:18*/
@Mapper // mybatis映射类
@Repository
public interface UsersMapper {List<Users> queryAllUsers();Users queryUserById(int userId);void addUser(Users users);void updateUser(Users users);void deleteUserById(int usersId);}
  • usersMapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.bitqian.mapper.UsersMapper"><select id="queryAllUsers" resultType="Users">select * from users1</select><select id="queryUserById" resultType="Users" parameterType="int">select * from users1 where userId = #{userId}</select><insert id="addUser" parameterType="Users">insert into users1 (userId, userName, userPassword) values (null, #{userName}, #{userPassword})</insert><update id="updateUser" parameterType="Users">update users1 set userName = #{userName}, userPassword = #{userPassword} where userId = #{userId}</update><delete id="deleteUserById" parameterType="Users">delete from users1 where userId = #{userId}</delete>
</mapper>
  • controller接口 restful风~
package cn.bitqian.controller;import cn.bitqian.entity.Users;
import cn.bitqian.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** @author echo lovely* @date 2020/10/25 11:00*/
@RestController
public class UsersController {@Autowiredprivate UsersMapper usersMapper;@GetMapping(value = "/users")public List<Users> queryAllUsers() {return usersMapper.queryAllUsers();}@GetMapping(value = "/user/{id}")public Users queryOneUser(@PathVariable(value = "id") Integer id) {return usersMapper.queryUserById(id);}@GetMapping(value = "/add")public String addUser() {Users users = new Users(null, "abcdefghi", "abc");usersMapper.addUser(users);return "ok";}@GetMapping(value = "/upd/{id}/{userName}/{userPassword}")public String updateUser(@PathVariable(value = "id") Integer id,@PathVariable(value = "userName") String userName,@PathVariable(value = "userPassword") String userPassword) {Users users = new Users(id, userName, userPassword);usersMapper.updateUser(users);return "ok";}@GetMapping(value = "/del/{id}")public String deleteUser(@PathVariable(value = "id") Integer id) {usersMapper.deleteUserById(id);return "ok";}}

application.properties 扩展配置,druid配置类

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_study?serverTimezone=GMT
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.filters=wall# mybatis 配置
# 别名
mybatis.type-aliases-package=cn.bitqian.entity
# mybatis 配置文件 要扫描的路径
mybatis.mapper-locations=classpath:mapper/*.xml
package cn.bitqian.config;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;/*** @author echo lovely* @date 2020/10/25 11:26*/
@Configuration
public class DruidConfig {// 自动装配@ConfigurationProperties(value = "spring.datasource")@Beanpublic DruidDataSource druidDataSource() {return new DruidDataSource();}// 注册 druid监控bean@Beanpublic ServletRegistrationBean druidServlet() {ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");Map<String, String> initParameters = new HashMap<>();initParameters.put("loginUserName", "admin");initParameters.put("loginPassword", "123");initParameters.put("allow", "127.0.0.1");bean.setInitParameters(initParameters);return bean;}
}

更新,fork我的代码,使用git bash,git bash here, git clone 项目地址。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/420561.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于android 调用网页隐藏地址栏

首先创建项目&#xff0c;在main.xml里 添加好WebView控件R.id为webview1。 HelloWebView.java 代码 package liu.ming.com; import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.webkit.WebView;import android.webkit.WebVie…

spring security 认证与权限控制

目录1. 使用授权和认证的必要性2. spring security 与 shiro 与 过滤器&#xff0c;拦截器3. 具体配置使用项目地址&#xff1a;https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-security-061. 使用授权和认证的必要性 什么是安全框架&#xff0c…

CodeIgniter框架下载辅助函数的一个小bug

if (strpos($_SERVER[HTTP_USER_AGENT], "MSIE") ! FALSE){header(Content-Type: .$mime); // <---1)这里header(Content-Disposition: attachment; filename".$filename.");header(Expires: 0);header(Cache-Control: must-revalidate, post-check0, p…

win7 cmd命令行窗口 宽度无法变大 自由调整大小

偶然遇到了这个问题,百度查到了解决方案,执行一个bat批处理命令. mode con lines40 mode con cols160 color 250 cls cmd转载于:https://www.cnblogs.com/DreamDrive/p/4017970.html

springboot 与shiro整合

shiro~ shiro快速入门springboot 整合shiro核心目标清爽pom用户认证授权认证&#xff0c;与数据库交互shiro configuration核心controller 获取shiro 中的token页面控制功能的隐藏和显示https://github.com/sevenyoungairye/spring-boot-study/tree/main/springboot-shiro-07sh…

jvm内存设置

摘抄自&#xff1a;http://www.cnblogs.com/fyj218/archive/2011/07/19/2110570.html 在eclipse根目录下打开eclipse.ini&#xff0c;默认内容为&#xff08;这里设置的是运行当前开发工具的JVM内存分配&#xff09;&#xff1a;-vmargs-Xms40m-Xmx256m-vmargs表示以下为虚拟机…

swagger接口文档使用

swagger接口文档一&#xff0c;swagger简介前后端分离swagger 诞生二&#xff0c;springboot集成swagger依赖编写helloworld接口配置swagger > config 配置类测试运行三&#xff0c;配置swaggerswagger 配置扫描接口如何做到只在生产环境中启动swagger&#xff1f;配置api文…

异步任务,邮箱任务,定时任务

task~异步任务邮箱任务定时任务源码下载异步任务 开启多线程&#xff0c;我飞了。 package cn.bitqian.service;import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service;/*** 异步任务 目的 多线程&#xff0c;交给spring托…

iframe在ipad safari的显示

今 天要在web中嵌套一个网址或本地HTML&#xff0c;用到了iframe&#xff0c;在电脑上设置scrolling‘auto’&#xff0c;宽度高度&#xff0c;会有滚动条出现。而在 ipad上会全部显示整个网页的宽度高度。scrolling属性无效。原来在html5中的iframe已经只有剩下src的属性。 但…

IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误&#xff0c;该错误简单的说&#xff0c;是由于 "ViewController" 还没有被加载&#xff0c;就调用该 ViewController 或者 ViewController 内的方法时&#xff0c;就会报这个错误。在不同…

maven传递依赖

目录1. 依赖传递2. 什么是依赖冲突3. 怎么解决4. 项目聚合maven是一个项目管理的工具&#xff0c;从项目的构建到项目开发&#xff0c;再到项目的测试&#xff0c;项目上线&#xff0c;都可一键管理。1. 那么&#xff0c;还有maven是如何管理项目中所用到的jar版本冲突&#xf…

使用apache FileUtils下载文件

目录工具代码实现测试工具 <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>或者 https://mvnrepository.com/artifact/commons-io/commons-io/2.7 然后放…

二分图多重匹配

在二分图的最大匹配中&#xff0c;每个点&#xff08;不管是X集合还是Y集合&#xff09;最多只能与一条匹配边相关联&#xff0c; 然而&#xff0c;经常有这种问题&#xff0c;即二分图的一个点可以和多条匹配边相关联&#xff0c;但有上限&#xff0c;即cap[i]表示点i最多能和…

springmvc,spring,hibernate5.0整合

目录1. pom依赖2. web.xml3. spring核心配置文件3.1 jdbc配置信息3.2 sping 配置文件4. 实体映射5. 项目结构5.1 curd5.2 页面6. 测试1. spring版本 5.1.5 RELEASE 2. hibernate版本 5.3.9.Final 3. 数据源使用c3p0项目使用eclipse2017 maven构建, 完成学生的新增&#xff0c;…

MYSQL 查看表上索引的 1 方法

前期准备&#xff1a; create table T9(A int ,B text,C text,fulltext index fix_test_for_T8_B(B));#在定义表的时候加索引 create unique index ix_test_for_T8_A on T9(A);#加朴素索引 create fulltext index fix_test_for_T8_C on T9(C);#加全文索引 --------------------…

springmvc 结合ajax批量新增

目录1. 需要注意的问题2. 页面代码3. controller定义参数接收1. 需要注意的问题 mvc框架的处理日期问题ResponseBody响应对象是自定义对象&#xff0c;响应不是jsonResopnseBody响应自定义对象时&#xff0c;日期为是long类型的数结束数据方法的参数&#xff0c;该如何定义&am…

吐槽一下Activiti的用户手册和一本书

业余没事的时候&#xff0c;看了点Java的资料&#xff0c;无意之中发现了Activiti&#xff0c;就打算自己跑几个例子看看到底是怎么回事。一直搞底层&#xff0c;也得偶尔关心下上层到底发展到什么程度了不是。悲惨的过程就是这么开始的&#xff0c;首先是Activiti的用户手册&a…

手写简单的启动器

starter1. target2. 手写启动器~2.1 自动装配&#xff0c;自定义属性2.2 启动器&#xff0c;引用自动装配模块3. 在自己的项目引用上面的starter1. target 1. 启动器只用来做依赖导入(导入配置模块)2. 专门来写一个自动配置模块3. 启动器依赖自动配置&#xff1b;别人只需要引入…