Springboot+mybatis搭建(以尽可能简单的,外加注释的方式)

时间:2021-12-26 05:11:23

感悟:写这篇文章的目的是网上很多相关的创建资料,但是我愣是没看懂,为什么呢,因为有很多没有注释的,对一知半解的我根本不懂,所以,我去了springboot的官网,看了一宿,终于搞到了半知半解。之后,我写了一个超简单的例子,就是下面你将看到的。目前我还浮在表面上,如果你想深入学习,请参照springboot官方网站,我相信你一定能学到不少东西。

我的环境:
Java: jdk1.8
IDE : Idea
Maven
mysql(我用的是这个数据库)
好了,咱们开始吧
Springboot+mybatis搭建(以尽可能简单的,外加注释的方式)

大家最好用idea或者eclipse,这样就能自动引入jar包进行开发了

数据表创建:
只需要一个user表即可

create table user(
name varchar(20),
age varchar(20),
password varchar(20)
);

我们先创建com.model
然后再下面写一个User.java

package com.model;

/**
* Created by hwt on 2017-01-24.
*/

public class User {
private String name;//用户姓名
private String age;//用户年龄
private String password;//用户密码

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

如果你熟悉ssm的话,下面我们创建com.mapper
在这个包的下面创建一个UserMapper.java(它是一个接口)

package com.mapper;

import org.apache.ibatis.annotations.Mapper;

/**
* Created by sa on 2017-01-24.
*/

@Mapper//说明这是个一个mapper
public interface UserMapper {
String selectAgeByName(String name);//根据姓名读取年龄
}

我们还需要写一个UserMapper.xml,用来对数据库进行操作

我们在resources下面创建一个mybatis文件夹,然后创建一个UserMapper.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.mapper.UserMapper" >

<select id="selectAgeByName" resultType="String">
SELECT age FROM USER WHERE name = #{ name,jdbcType=VARCHAR}
</select>

</mapper>

到了这里,数据库的操作基本上完成了,后边我们只需要配置一些参数即可
下面我们来写一个UserController
我们先创建一个com.controller包,在它的下面创建一个UserController.java

package com.controller;

import com.mapper.UserMapper;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* Created by sa on 2017-01-24.
*/

@Controller//这个注解说明这是controller层
public class UserController
{


@Autowired
private UserMapper userMapper;

@Autowired
private UserService userService;

@RequestMapping(value = "/login")//映射login
public String userLogin()
{
//String age=userService.selectAgeByName("hwt");//通过userService来获取
String age=userMapper.selectAgeByName("hwt");//通过userMapper来获取
System.out.println("====================");
System.out.println(age);//打印年龄
return "login";
}
}

因为springboot是依靠main函数启动的,那么我们需要写一个Application.java这个启动类
我们可以在com这个包下创建Application.java这个函数

package com;

import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
* Created by sa on 2017-01-24.
*/

@SpringBootApplication//自动加载配置文件 因为配置了插件maven会自动扫描resources下面的yaml文件,如果用了properties,则扫描的是application.properties
@EnableTransactionManagement//开启事务管理
@MapperScan("com.mapper")//扫描包
public class Application
{

public static void main(String args[])
{
SpringApplication.run(Application.class);//启动
}
}

好了,到了这里我们还有两步走,一步是写相关的配置策略,另一步是写一个页面

我们在resources下面创建一个application.yaml文件
如果你不懂什么是yaml的话,可以参看springboot官方文档
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
这里面会告诉你什么是yaml文件

server:
port: 8080
spring:
velocity:
charset: UTF-8
properties.input.encoding: UTF-8
properties.output.encoding: UTF-8
resourceLoaderPath: classpath:/pages/
prefix: /
# toolbox-config-location: /WEB-INF/toolbox.xml
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
minIdle: 1
maxActive: 20
initialSize: 1
timeBetweenEvictionRunsMillis: 3000
minEvictableIdleTimeMillis: 300000
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
mybatis:
mapperLocations: classpath*:mapper/*.xml
typeAliasesPackage: com.model

最后我们再在resources下面创建一个pages文件夹
里面写一个网页就可以了login.vm

<!DOCTYPE html>
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Meta, title, CSS, favicons, etc. -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Gentallela Alela! | </title>

<!-- Bootstrap core CSS -->

<link href="css/bootstrap.min.css" rel="stylesheet">

<link href="fonts/css/font-awesome.min.css" rel="stylesheet">
<link href="css/animate.min.css" rel="stylesheet">

<!-- Custom styling plus plugins -->
<link href="css/custom.css" rel="stylesheet">
<link href="css/icheck/flat/green.css" rel="stylesheet">


<script src="js/jquery.min.js"></script>

<!--[if lt IE 9]>
<script src="../assets/js/ie8-responsive-file-warning.js"></script>
<![endif]-->


<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->


</head>

<body style="background:#F7F7F7;">

<p>login</p>
</body>

</html>

忘记了,还需要配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>spring-boot</groupId>
<artifactId>springboot-test</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
</dependency>

<!--支持使用 JDBC 访问数据库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>

<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>

</dependencies>

<!-- 用maven需要安装一个插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

好了,完成了,你可能需要在application.yaml中修改数据库账号和密码
然后运行application.java,在网页中输入127.0.0.1:8080即可,端口号也可以在application.yaml修改