前言
spring boot 极大的简化了项目配置,比传统的SSM框架的开发效率更高。
开发准备
- jdk1.8
- maven 3.5.0
- IDEA
开发步骤:
1. 新建Spring Boot项目
1.1 Intellij idea菜单栏File->new->project。
1.2选择左侧栏中spring initializr,右侧选择jdk版本,以及默认的Service URL,点击next。
1.3然后填写项目的Group、Artifact等信息,点击next。
1.4左侧点击Web,中间一侧选择Web,然后左侧选择SQL,中间一侧选择JDBC、Mybatis、MYSQL(数据库用的是mysql,大家可以选择其他DB),点击next。
1.5 至此,一个maven web项目就创建好了,目录结构如下。
2. 创建文件夹形成web目录结构
2.1 创建以下典型的web目录结构
3. 修改属性配置文件
3.1 将 application.properties
文件修改为application.yml
文件
原因:如果使用的是默认的applica.properties
文件,会重复写许多配置式的前缀,并且不能够体现出结构化特点。
3.2 在pom.xml中添加依赖,防止默认寻找application.properties文件出错
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3.3 在application.yml文件中写入配置
server:
port: 8081 //访问端口号
spring:
datasource: //数据库配置
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/angulardemo
username: root
password: wmj
mybatis: //Mybatis相关配置
typeAliasesPackage: com.wmj.bootMybatis.Entity //指明实体位置
mapperLocations: classpath:Mapper/*.xml //配置Mapper.xml文件地址
4. 在Spring Boot启动类中声明Dao层的位置
package com.wmj.bootMybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.wmj.bootMybatis.Dao")//非常重要
public class BootMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(BootMybatisApplication.class, args);
}
}
5. 像传统的是SSM开发模式进行开发
5.1 开发目录结构如下
5.2 Entity层
package com.wmj.springbootMybatis.Entity;
public class Hero {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5.2 Dao层
package com.wmj.springbootMybatis.Dao;
import com.wmj.springbootMybatis.Entity.Hero;
import java.util.List;
public interface HeroDao {
//获取所有英雄信息
List<Hero> getAllHeroInfo();
//获取所有英雄信息
List<Hero> getHeroInfoById(Integer id);
//获取查询英雄信息
List<Hero> getHeroInfoByName(String name);
//修改英雄信息
int updateHeroInfo(Hero heroInfo);
//获取现有英雄的最大ID
int getMaxHeroId();
//添加英雄信息
int addHeroInfo(Hero hero);
//根据学号删除英雄信息
int deleteHeroInfo(Integer id);
}
5.3 Dao层对应Mapper.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.wmj.springbootMybatis.Dao.HeroDao" >
<resultMap id="heroResultMap" type="com.wmj.springbootMybatis.Entity.Hero" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<select id="getAllHeroInfo" parameterType="java.lang.Integer" resultMap="heroResultMap">
select id,name from herotable
</select>
<select id="getHeroInfoById" parameterType="java.lang.Integer" resultMap="heroResultMap">
select id,name from herotable where id = #{id}
</select>
<select id="getHeroInfoByName" parameterType="java.lang.String" resultMap="heroResultMap">
select id,name from herotable where name like CONCAT('%',#{name},'%')
</select>
<update id="updateHeroInfo" parameterType="com.wmj.springbootMybatis.Entity.Hero">
update herotable
<set >
<if test="name != null" >
name = #{name},
</if>
</set>
where id = #{id}
</update>
<select id="getMaxHeroId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
select Max(id) from herotable
</select>
<insert id="addHeroInfo" parameterType="com.wmj.springbootMybatis.Entity.Hero" >
insert into herotable values (#{id}, #{name})
</insert>
<delete id="deleteHeroInfo" parameterType="java.lang.Integer" >
delete from herotable
where id = #{id}
</delete>
</mapper>
5.4 Service层接口
package com.wmj.springbootMybatis.Service;
import com.wmj.springbootMybatis.Entity.Hero;
import java.util.List;
public interface HeroService {
//获取所有hero
public List<Hero> getAllHeroInfo();
//根据id获取hero,返回List
public List<Hero> getStudentInfoById(int id);
//根据name获取hero,返回List
public List<Hero> getStudentInfoByName(String name);
//修改Hero信息
int updateHeroInfo(Hero heroInfo);
//获取hero的最大Id
int getMaxHeroId();
//添加Hero信息
int addHeroInfo(Hero heroInfo);
//根据id删除Hero信息
int deleteHeroInfo(Integer id);
}
5.5 Service层实现层
package com.wmj.springbootMybatis.Service.Impl;
import com.wmj.springbootMybatis.Dao.HeroDao;
import com.wmj.springbootMybatis.Entity.Hero;
import com.wmj.springbootMybatis.Service.HeroService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service("heroService")
public class HeroServiceImpl implements HeroService {
@Resource
private HeroDao heroDao;
@Override
public List<Hero> getAllHeroInfo() {
List<Hero> stuInfo =this.heroDao.getAllHeroInfo();
return stuInfo;
}
@Override
public List<Hero> getStudentInfoById(int id) {
List<Hero> stuInfo =this.heroDao.getHeroInfoById(id);
return stuInfo;
}
@Override
public List<Hero> getStudentInfoByName(String name) {
List<Hero> stuInfo =this.heroDao.getHeroInfoByName(name);
return stuInfo;
}
@Override
public int updateHeroInfo(Hero heroInfo) {
return this.heroDao.updateHeroInfo(heroInfo);
}
@Override
public int getMaxHeroId() {
return this.heroDao.getMaxHeroId();
}
@Override
public int addHeroInfo(Hero heroInfo) {
return this.heroDao.addHeroInfo(heroInfo);
}
@Override
public int deleteHeroInfo(Integer id) {
return this.heroDao.deleteHeroInfo(id);
}
}
5.6 Service层实现层
package com.wmj.springbootMybatis.Controller;
import com.wmj.springbootMybatis.Entity.Hero;
import com.wmj.springbootMybatis.Service.HeroService;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/hero")
public class HeroInfoController {
@Resource
private HeroService heroSerive;
@RequestMapping(value="/selectAllHero")
public List<Hero> selectAllHero(HttpServletRequest request, HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*"); //解决跨域问题
List<Hero> allHeroInfo=heroSerive.getAllHeroInfo();
return allHeroInfo;
}
@RequestMapping(value="/selectHeroById")
public List<Hero> selectHeroById(HttpServletRequest request, HttpServletResponse response,String heroId){
response.setHeader("Access-Control-Allow-Origin", "*");
List<Hero> HeroInfo=heroSerive.getStudentInfoById(Integer.parseInt(heroId));
return HeroInfo;
}
@RequestMapping(value="/selectHeroByName")
public List<Hero> selectHeroByName(HttpServletRequest request, HttpServletResponse response,String heroName){
response.setHeader("Access-Control-Allow-Origin", "*");
List<Hero> HeroInfo = new ArrayList<Hero>();
if(heroName.equals("")){
}
else{
HeroInfo=heroSerive.getStudentInfoByName(heroName);
}
return HeroInfo;
}
@RequestMapping(value="updateHeroById")
public JSONObject updateStudentInfo(HttpServletRequest request, HttpServletResponse response, String hero) {
response.setHeader("Access-Control-Allow-Origin", "*");
JSONObject jsonobject = JSONObject.fromObject(hero);
Hero rule = (Hero) JSONObject.toBean(jsonobject, Hero.class);
int result = heroSerive.updateHeroInfo(rule);
JSONObject state = new JSONObject();
if(result ==1){
state.put("state", "修改成功");
}
else{
state.put("state", "修改失败");
}
return state;
}
@RequestMapping(value="/addHeroInfo")
public List<Hero> addStudentInfo(HttpServletRequest request, HttpServletResponse response, String name){
response.setHeader("Access-Control-Allow-Origin", "*");
// JSONObject jsonobject = JSONObject.fromObject(json);
//获取英雄的最大ID
int heroMaxId = heroSerive.getMaxHeroId();
int heroId = heroMaxId+1;
JSONObject heroInfo = new JSONObject();
heroInfo.put("id", heroId);
heroInfo.put("name", name);
Hero rule = (Hero) JSONObject.toBean(heroInfo, Hero.class);
heroSerive.addHeroInfo(rule);
List<Hero> HeroInfo=heroSerive.getStudentInfoById(heroId);
return HeroInfo;
}
@RequestMapping(value="deleteHeroById")
public JSONObject deleteHeroById(HttpServletRequest request, HttpServletResponse response, String id) {
response.setHeader("Access-Control-Allow-Origin", "*");
int result = heroSerive.deleteHeroInfo(Integer.parseInt(id) );
JSONObject state = new JSONObject();
if(result ==1){
state.put("state", "删除成功");
}
else{
state.put("state", "删除失败");
}
return state;
}
}