2024年spring boot mybatis 整合教程,资深大牛带你了解源码

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

image

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

com.alibaba

druid

1.1.0

org.springframework.boot

spring-boot-maven-plugin

在application.yml中添加数据源、Mybatis的实体和配置文件位置。

#默认使用配置

spring:

profiles:

active: dev

#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources

mybatis:

typeAliasesPackage: com.xdd.entity

mapperLocations: classpath:mapper/*.xml


#开发配置

spring:

profiles: dev

datasource:

url: jdbc:mysql://localhost:3306/test

username: root

password: root

driver-class-name: com.mysql.jdbc.Driver

使用druid数据源

type: com.alibaba.druid.pool.DruidDataSource

就这样就整合完成了!我们测试一下。

用MyBatis Generator自动生成代码,参考博文:http://blog.csdn.net/zhshulin/article/details/23912615 这里列一下自动生成的代码。

import com.xdd.entity.User;

import org.springframework.stereotype.Component;

public interface UserDao {

int deleteByPrimaryKey(Integer id);

int insert(User record);

int insertSelective(User record);

User selectByPrimaryKey(Integer id);

int updateByPrimaryKeySelective(User record);

int updateByPrimaryKey(User record);

}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

id, user_name, password, age

select

from user_t

where id = #{id,jdbcType=INTEGER}

delete from user_t

where id = #{id,jdbcType=INTEGER}

insert into user_t (id, user_name, password,

age)

values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},

#{age,jdbcType=INTEGER})

insert into user_t

id,

user_name,

password,

age,

#{id,jdbcType=INTEGER},

#{userName,jdbcType=VARCHAR},

#{password,jdbcType=VARCHAR},

#{age,jdbcType=INTEGER},

update user_t

user_name = #{userName,jdbcType=VARCHAR},

password = #{password,jdbcType=VARCHAR},

age = #{age,jdbcType=INTEGER},

where id = #{id,jdbcType=INTEGER}

update user_t

set user_name = #{userName,jdbcType=VARCHAR},

password = #{password,jdbcType=VARCHAR},

age = #{age,jdbcType=INTEGER}

where id = #{id,jdbcType=INTEGER}

public class User {

private Integer id;

private String userName;

private String password;

private Integer age;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName == null ? null : userName.trim();

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password == null ? null : password.trim();

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

最后将DemoApplication.java修改一下,让其扫描dao层接口。

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication

@MapperScan(“com.xdd.dao”)

public class DemoApplication extends SpringBootServletInitializer{

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class,args);

}

}

自己添加controller和service

import java.util.List;

import java.util.Map;

public interface UserService {

public User getUserById(int userId);

boolean addUser(User record);

}

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import java.util.Map;

@Service(“userService”)

public class UserServiceImpl implements UserService {

@Resource

private UserDao userDao;

public User getUserById(int userId) {

return userDao.selectByPrimaryKey(userId);

}

public boolean addUser(User record){

boolean result = false;

try {

userDao.insertSelective(record);

result = true;

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

}

@Controller

@RequestMapping(“/user”)

public class UserController {

@Resource

private UserService userService;

@RequestMapping(“/showUser”)

@ResponseBody

public User toIndex(HttpServletRequest request, Model model){

int userId = Integer.parseInt(request.getParameter(“id”));

User user = this.userService.getUserById(userId);

return user;

}

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

= Integer.parseInt(request.getParameter(“id”));

User user = this.userService.getUserById(userId);

return user;

}

一线互联网大厂Java核心面试题库

[外链图片转存中…(img-NYghdthy-1715007649579)]

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值