用Eclipse创建一个spring boot(连接mysql,简单使用mybatis)
Spring boot
- 1、连接mysql(用的mybaits)
- (1)配置pom.xml依赖
- (2)配置application.properties
- (3)创建实体类
- (4)写mapper的配置文件
- (5)写Mapper映射文件
- (6)写服务
- (7)配置Controller
- (8)补充DidididiApplication文件
1、连接mysql(用的mybaits)
(1)配置pom.xml依赖
<!--添加MySql依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency><!-- 添加mybatis依赖 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.2</version></dependency>
(2)配置application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/这里是数据库名?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=这里是账号
spring.datasource.password=这里是密码
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mappering/*.xml
(3)创建实体类
创建entities、services、mapper文件夹,具体位置都在demo文件夹下。在resources下建mappering文件夹。
在entities文件夹内新建一个loginUser文件:
package com.example.demo.entities;public class loginUser {private String username;private int password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public int getPassword() {return password;}public void setPassword(int password) {this.password = password;}public loginUser(){};public loginUser(String name,int pwd) {this.password = pwd;this.username = name;}@Overridepublic String toString() {return "User [username=" + username + ", password=" + password + "]";}
}
(4)写mapper的配置文件
在mappering文件夹里创建UserMappring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><resultMap id="BaseResultMap" type="com.example.demo.entities.loginUser"><result column="id" jdbcType="INTEGER" property="id" /><result column="username" jdbcType="VARCHAR" property="username" /><result column="password" jdbcType="INTEGER" property="password" /></resultMap><select id="Sel" resultType="com.example.demo.entities.loginUser">select * from User where id = #{id}</select>
</mapper>
(5)写Mapper映射文件
在mapper文件夹里创建一个UserMapper.class文件
package com.example.demo.mapper;import org.springframework.stereotype.Repository;
import com.example.demo.entities.loginUser;
@Repository
public interface UserMapper {loginUser Sel(int id);
}
(6)写服务
在services文件夹里创建一个UserService文件夹
package com.example.demo.services;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import com.example.demo.entities.loginUser;
import com.example.demo.mapper.*;@Service
public class UserService {@AutowiredUserMapper usermapper;public loginUser Sel(int id){return usermapper.Sel(id);}
}
(7)配置Controller
打开我们之前创建的testController。连接
package com.example.demo.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.example.demo.services.UserService;@RestController
@RequestMapping("/testBoot")
public class testController {@Autowiredprivate UserService userService;@RequestMapping("/hello")public String hello() {return "Hello Spring Boot!";}@RequestMapping("/getUser")public String GetUser(){System.out.print("到这里了");return userService.Sel(0).toString();}
}
(8)补充DidididiApplication文件
package com.example.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DidididiApplication {public static void main(String[] args) {SpringApplication.run(DidididiApplication.class, args);}}
用Eclipse创建一个spring boot(连接mysql,简单使用mybatis)相关推荐
- 用Eclipse创建一个spring boot(创建,并配好controller)
Spring Boot 1.建立文件 2.创建Controller 3.打开页面尝试 1.建立文件 打开eclipse ->上面一栏file ->new ->Spring Start ...
- eclipse创建springboot项目_创建一个 Spring Boot 项目,你会几种方法?
我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...
- 创建一个Spring Boot项目
文章目录 Spring Boot的功能 Spring Boot的优点 Spring Boot 的三种创建方式 在线创建 使用开发工具创建 IntelliJ IDEA Maven 创建 项目结构 修改T ...
- 一个项目有两个pom_实现一个Spring Boot Starter超简单,读 Starter 源码也不在话下...
Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...
- 实现一个 Spring Boot Starter 原来如此简单,读 Starter 源码也不在话下
我是风筝,公众号「古时的风筝」,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者.现已转行程序员鼓励师 Spring Cloud 系列文章已经完成,可以 ...
- spring boot连接mysql数据库
一.pom.xml添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifactI ...
- Spring Boot连接MySQL报错“Internal Server Error”的解决办法
报错信息如下: {timestamp: "2018-06-14T03:48:23.436+0000", status: 500, error: "Internal Ser ...
- 菜鸟教程 | IDEA创建一个spring boot项目的详细过程
目录 1.新建项目 2.选择项目所需依赖 3.手动导入部分依赖 创建spring项目的详细过程~ 1.新建项目 file --> new --> project groupid 和 ar ...
- Spring boot 连接MySQL,使用数据源hikaricp
1 .添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
最新文章
- Weiss的数据结构与算法分析(C++版)源码编译说明
- python 获取本机IP的三种方式
- ConcurrentHashMap的源码分析-put方法第二阶段
- 【文章】工作之余,放松心情
- 后端开发(1)---大话后端开发的技巧大集合
- 最短寻道时间算法c语言,如果北京到上海有千亿条路,寻找最短路径用C语言编程用枚举法没效率,应该用什么算法才能高效解决它?...
- 六款值得推荐的数据挖掘得力助手
- Linux查询命令帮助语句,linux有关命令的帮助和用法查看
- lacp静态和动态区别_TTM、静态和动态市盈率的区别,三种市盈率看哪个?
- 12306全国火车站点所在区划代码经纬度数据,精确到县区级
- hpm1216nfh驱动程序_惠普m1216nfh
- 微信公众号 自定义菜单跳转html页面 前端获取code,openid
- css 1倍图 2倍图 3倍图
- Qt5使用Poppler实现PDF阅读器
- java开发用固态还是机械硬盘_搭载固态硬盘的服务器究竟比搭机械硬盘快多少?...
- Invalid configuration of tez jars, tez.lib.uris is not defined in the configuration
- 简明扼要理解YOLO v3
- PAKDD 2019 都有哪些重要看点?看这篇文章就够了!
- 人工智能 识别医疗票据、医疗结算清单 OCR识别技术
- 国企计算机技术岗面试题目,国企历年经典面试试题+答案(二)