最近在学习javaSpringBoot,后端集成以下插件,mysql,mybatis,pagehelper,前端使用vue,ui是使用elementui,使用前后端分离的形式,废话不多说,上个效果图
@CrossOrigin(origins = “*”,maxAge = 3600)// 解决跨域
这边先搭建springBoot的项目
1创建项目
这边自己起个名字
选择依赖 主要是Spring Web, MyBatis Framework,MySQL Driver
项目名称和项目保存的路径
项目建好了,下面我们来加载依赖
打开pom.xml,如果你数据库版本是最新的,就不用改版本
可以查看自己电脑依赖的版本
本案例不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties), 文件的内容如下(此处只配置最基本的)
server:
port: 9000 # 端口号
spring:
datasource: # 数据库连接
name: test
url: jdbc:mysql://127.0.0.1:3306/spring_test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis: # mybatis连接
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.allen.pojo
创建数据库
配置mybatis generator 自动生成代码插件
配置pom.xml中generator 插件所对应的配置文件 ${basedir}/src/main/resources/generator/generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="C:\Users\Allen\.m2\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!--数据库连接驱动类,URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/spring_test" userId="root" password="root">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成(实体)模型的包名和位置-->
<javaModelGenerator targetPackage="com.allen.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成XML映射文件的包名和位置-->
<sqlMapGenerator targetPackage="resources.mapping" targetProject="src/main">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO接口的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.allen.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="userInfo" domainObjectName="UserInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
在pom.xml里面加入mybatis generator 自动生成代码插件
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
加入generator插件右边plugin会生成mybatis-generator的一个文件,没有就上面刷新下,然后点击运行生成代码
打开类DemoApplication.java,这个是springboot的启动类。我们需要添加点东西:
package com.allen.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@MapperScan("com.allen.dao")
@ComponentScan(basePackages = {"com.allen.*"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
下面我们在dao下的UserInfoMapper加个findAllUser方法
package com.allen.dao;
import com.allen.pojo.UserInfo;
import java.util.List;
public interface UserInfoMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int deleteByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int insert(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int insertSelective(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
UserInfo selectByPrimaryKey(Integer userid);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int updateByPrimaryKeySelective(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int updateByPrimaryKey(UserInfo record);
// 查询全部的方法
List findAllUser();
}
打开rresources下的mapping下的UserInfoMapper.xml实现fillUserAll()这个方法
<?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.allen.dao.UserInfoMapper" >
<resultMap id="BaseResultMap" type="com.allen.pojo.UserInfo" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="userId" property="userid" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
userId, user_name, phone, sex
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from userinfo
where userId = #{userid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from userinfo
where userId = #{userid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.allen.pojo.UserInfo" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo (userId, user_name, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.allen.pojo.UserInfo" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into userinfo
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userid != null" >
userId,
</if>
<if test="userName != null" >
user_name,
</if>
<if test="phone != null" >
phone,
</if>
<if test="sex != null" >
sex,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userid != null" >
#{userid,jdbcType=INTEGER},
</if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.allen.pojo.UserInfo" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
</set>
where userId = #{userid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.allen.pojo.UserInfo" >
<!--
WARNING - @mbggenerated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update userinfo
set user_name = #{userName,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR}
where userId = #{userid,jdbcType=INTEGER}
</update>
<select id="fillUserAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from userinfo
</select>
</mapper>
创建service文件夹跟controller文件夹
ServiceImpl.java里面的文件如下
package com.allen.service.impl;
import com.allen.dao.UserInfoMapper;
import com.allen.pojo.UserInfo;
import com.allen.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ServiceImpl implements Services {
@Autowired
private UserInfoMapper userDao;// 这里会报错,但是不影响
@Override
public List<UserInfo> fillUserAll() {
return userDao.fillUserAll();
}
}
如果强迫症看不下去上面那个报错:(解决方法)
Services文件内容如下
package com.allen.service;
import com.allen.pojo.UserInfo;
import java.util.List;
public interface Services {
List<UserInfo> fillUserAll();
}
下面我们来看下controller的文件
package com.allen.controller;
import com.allen.pojo.UserInfo;
import com.allen.service.Services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value={"/user"})
public class UserController {
@Autowired
private Services services;
@RequestMapping(value = {"/findAll"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
public List<UserInfo> fillUserAll(){
return services.fillUserAll();
}
}
然后点击运行,在浏览器输入localhost:9000/user/fillAll
讲到这边后端的已经跑起来了,下一节我们来讲前端跟后端如何取值,做对userinfo这个表的增删改查
后台代码链接
链接:https://pan.baidu.com/s/1-c9jyUIAVDUNagg7te7tDQ
提取码:me52