maven springboot集成mybatis构建项目

曾经有过用spring springMVC Hibernate(SSH)框架开发项目的经验,每次看见xml配置文件就头疼。开学之初,在曾师兄的推荐下开始折腾spring boot,学习spring boot就像在黑暗中看到了光明一样,真爽,头也不疼了。今天将会介绍如何整合springboot和mybatis。

环境介绍

在准备搭建项目之前,我们需要准备好开发工具及其环境。

  • JDK
  • eclipse
  • Maven
    关于上面的安装及其配置,这里不再介绍,网上有很多教程,可以完成。

在eclipse中生成spring boot项目

在eclipse中添加spring tool插件

  1. Help -> Eclipse Marketplace
  2. 选择“Popular”标签去查找Spring Tools 3 Add-On插件,找到后在线安裝
    Spring Tools 3 Add-On插件
    同样,自己也可以官网下载插件后在本地进行安装。

创建spring boot应用

  1. New -> Project
  2. Spring Boot ->Sping Starter Project
    demo
  3. 填写项目的信息,其中由于我们用maven管理项目,需要将type选择Maven,其他根据自己填写
    demo2
  4. 选择需要Dependency,我们这里需要选择MysqlDriver,Mybatis,Spring Web Starter
    demo3
    5.将version 2.1.5 改为2.1.4,不要问我为什么,因为我用2.1.5一直有错,我就强行给他改了
    demo4

项目结构

demo5

修改配置文件以及编码

在application.properties文件中添加mybatis的配置信息

# 端口
server.port=8080

# mybatis配置
#url
spring.datasource.url=jdbc:mysql://localhost:3306/test1610?characterEncoding=utf-8&useSSL=true
#用户名
spring.datasource.username=root
#密码
spring.datasource.password=root
#驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

bean中的代码

//User.java

package top.datablog.bean;


public class User {
	private int id;
	private String name;
	private String sex;
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
}


controller中的代码

//Student.java

package top.datablog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import top.datablog.bean.User;
import top.datablog.service.UserService;

@RestController
public class Student {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/")
	public String hello() {
		return "你好";
	}
	@RequestMapping("/user")
	public List<User> findAllUser(){
		return userService.findAllUser();
	}
}

mapper中的代码

//UserRepository.java

package top.datablog.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import top.datablog.bean.User;

@Mapper
public interface UserRepository {

	@Select("select * from mou_user")
	List<User> findAllUser();
}

service中的代码

//UserService.java

package top.datablog.service;

import java.util.List;

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

import top.datablog.bean.User;
import top.datablog.mapper.UserRepository;

@Service
public class UserService {
	@Autowired
	private UserRepository userRepository;
	
	public List<User> findAllUser(){
		return userRepository.findAllUser();
	}
}

运行项目

运行DemoApplication 中的main方法:Run As -> Spring Boot App)
demo6
DemoApplication的代码


package top.datablog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

打开浏览器访问

http://localhost:8080/user
demo7

附上数据库表的结构

demo8

参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值