一、创建数据库
create table t_goods(
goods_id int primary key auto_increment,
goods_name varchar(20),
goods_price double,
goods_imgpath varchar(50)
);
二、创建SpringBoot项目
三、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 引入 Druid 数据源依赖:https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
四、创建javabean
package com.wangxing.springboot.bean;
public class GoodBean {
private int goodsid;
private String goodsname;
private double goodsprice;
private String goodsimgpath;
public int getGoodsid() {
return goodsid;
}
public void setGoodsid(int goodsid) {
this.goodsid = goodsid;
}
public String getGoodsname() {
return goodsname;
}
public void setGoodsname(String goodsname) {
this.goodsname = goodsname;
}
public double getGoodsprice() {
return goodsprice;
}
public void setGoodsprice(double goodsprice) {
this.goodsprice = goodsprice;
}
public String getGoodsimgpath() {
return goodsimgpath;
}
public void setGoodsimgpath(String goodsimgpath) {
this.goodsimgpath = goodsimgpath;
}
}
ResBean.java
package com.wangxing.springboot.bean;
public class ResBean {
private boolean success;
private String msg;
public ResBean(boolean b, String s) {
this.success = b;
this.msg = s;
}
public boolean isSuccess() {
return success;
}
public String getMsg() {
return msg;
}
}
五、创建数据访问接口
package com.wangxing.springboot.mapper;
import com.wangxing.springboot.bean.GoodBean;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
//@Mapper
public interface GoodsMapper {
//添加信息的数据访问接口
void insertGoods(GoodBean goodBean);
//修改信息的数据访问接口
void updateGoods(GoodBean goodBean);
//删除信息的数据访问接口
void deleteGoods(int goodsId);
//根据id查询商品信息的数据访问接口
GoodBean selectGoodsById(int goodsId);
//查询所有商品信息的数据访问接口
List<GoodBean> selectGoodsAll();
}
六、在src/main/resources/mapper/GoodsMappe.xml创建SQL映射文件
<?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.wangxing.springboot.mapper.GoodsMapper">
<insert id="insertGoods" parameterType="com.wangxing.springboot.bean.GoodBean">
insert into t_goods value (null,#{goodsname},#{goodsprice},#{goodsimgpath})
</insert>
<update id="updateGoods" parameterType="com.wangxing.springboot.bean.GoodBean">
update t_goods set
goods_name=#{goodsname},goods_price=#{goodsprice},goods_imgpath=#{goodsimgpath}
where goods_id=#{goodsid}
</update>
<delete id="deleteGoods" parameterType="java.lang.Integer">
delete from t_goods where goods_id=#{goodsid}
</delete>
<select id="selectGoodsById" parameterType="java.lang.Integer"
resultMap="goodsMap">
select * from t_goods where goods_id=#{id}
</select>
<select id="selectGoodsAll" parameterType="java.util.List" resultMap="goodsMap">
select * from t_goods
</select>
<resultMap id="goodsMap" type="com.wangxing.springboot.bean.GoodBean">
<id column="goods_id" property="goodsid"></id>
<result column="goods_name" property="goodsname"></result>
<result column="goods_price" property="goodsprice"></result>
<result column="goods_imgpath" property="goodsimgpath"></result>
</resultMap>
</mapper>
七、在src/main/resources/config/mybatis-config.xml创建MyBatis核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
八.创建Druid数据源配置类
package com.wangxing.springboot.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = "spring.druiddatasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
}
九、创建MyBatis配置类
package com.wangxing.springboot.config;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
//匿名内部类
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
十、在SpringBoot的核心配置文件[application.properties]中配置数据源参数.....
#将springBoot默认的数据源用alibaba的Druid代替
spring.druiddatasource.type=com.alibaba.druid.pool.DruidDataSource
#配置alibab的Druid数据源参数
spring.druiddatasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.druiddatasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.druiddatasource.username=root
spring.druiddatasource.password=123456
spring.druiddatasource.initialSize=5
spring.druiddatasource.minIdle=5
spring.druiddatasource.maxActive=20
spring.druiddatasource.maxWait=60000
spring.druiddatasource.timeBetweenEvictionRunsMillis=60000
spring.druiddatasource.minEvictableIdleTimeMillis=300000
spring.druiddatasource.validationQuery=SELECT 1 FROM DUAL
spring.druiddatasource.testWhileIdle=true
spring.druiddatasource.testOnBorrow=false
spring.druiddatasource.testOnReturn=false
spring.druiddatasource.poolPreparedStatements=true
#配置MyBatis核心配置文件的位置
#mybatis.config-location=classpath:config/mybatis-config.xml
#配置SQL映射文件的位置
mybatis.mapper-locations=classpath:mapper/*.xml
十一、创建业务访问接口以及实现类
package com.wangxing.springboot.service;
import com.wangxing.springboot.bean.GoodBean;
import java.util.List;
public interface GoodsService {
//添加信息的数据访问接口
void insertGoods(GoodBean goodBean);
//修改信息的数据访问接口
void updateGoods(GoodBean goodBean);
//删除信息的数据访问接口
void deleteGoods(int goodsId);
//根据id查询商品信息的数据访问接口
GoodBean selectGoodsById(int goodsId);
//查询所有商品信息的数据访问接口
List<GoodBean> selectGoodsAll();
}
实现类
package com.wangxing.springboot.service.impl;
import com.wangxing.springboot.bean.GoodBean;
import com.wangxing.springboot.mapper.GoodsMapper;
import com.wangxing.springboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {
@Autowired
private GoodsMapper goodsMapper;
@Override
public void insertGoods(GoodBean goodBean) {
goodsMapper.insertGoods(goodBean);
}
@Override
public void updateGoods(GoodBean goodBean) {
goodsMapper.updateGoods(goodBean);
}
@Override
public void deleteGoods(int goodsId) {
goodsMapper.deleteGoods(goodsId);
}
@Override
public GoodBean selectGoodsById(int goodsId) {
return goodsMapper.selectGoodsById(goodsId);
}
@Override
public List<GoodBean> selectGoodsAll() {
return goodsMapper.selectGoodsAll();
}
}
十二、创建控制器
package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.GoodBean;
import com.wangxing.springboot.bean.ResBean;
import com.wangxing.springboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/test1")
public class GoodsController {
@Autowired
private GoodsService goodsService;
/**
* 添加商品信息的控制层方法
* @param goodBean
* @return
*/
@RequestMapping("/add.do")
public ResBean addGoods(@RequestBody GoodBean goodBean){
try {
goodsService.insertGoods(goodBean);
return new ResBean(true,"添加成功!");
} catch (Exception e) {
e.printStackTrace();
return new ResBean(false,"添加失败!");
}
}
/**
* 修改商品信息的方法
* @param goodBean
* @return
*/
@RequestMapping("/update.do")
public ResBean update(@RequestBody GoodBean goodBean){
try {
goodsService.updateGoods(goodBean);
return new ResBean(true,"修改成功!");
} catch (Exception e) {
e.printStackTrace();
return new ResBean(false,"修改失败!");
}
}
/**
* 删除商品信息
* @param goodsid
* @return
*/
@RequestMapping("/dalete.do")
public ResBean dalete(int goodsid){
try {
goodsService.deleteGoods(goodsid);
return new ResBean(true,"删除成功!");
} catch (Exception e) {
e.printStackTrace();
return new ResBean(false,"删除失败!");
}
}
/**
* 根据ID查询
* @param goodsid
* @return
*/
@RequestMapping("/findOne.do")
public GoodBean finOneGoods(int goodsid){
return goodsService.selectGoodsById(goodsid);
}
/**
* 查询所有商品信息
* @return
*/
@RequestMapping("/findList.do")
public List<GoodBean> findListGoods(){
return goodsService.selectGoodsAll();
}
}
十三、配置主类
package com.wangxing.springboot;
import com.wangxing.springboot.config.DruidConfig;
import com.wangxing.springboot.config.MyBatisConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@ComponentScan(basePackages = "com.wangxing")
@MapperScan("com.wangxing.springboot.mapper")
@Import({MyBatisConfig.class, DruidConfig.class})
public class SpringbootSsmApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootSsmApplication.class, args);
}
}
十四、导入jquery的函数库文件
十五、创建测试用的html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试SSM</title>
<script src="js/jquery-3.6.0.js"></script>
<script>
$(function () {
$("#but1").click(function () {
var data = {"goodsid":null,"goodsname":"苹果","goodsprice":5990,"goodsimgpath":"imgs/shouji3.jpg"};
var goodsjson = JSON.stringify(data);
$.ajax({
url:"../test1/add.do",
data:goodsjson,
type:"POST",
dataType:"json",
contentType:"application/json",
success:function (response) {
alert(response.msg);
}
});
});
// 修改商品信息
$("#but2").click(function () {
var data = {"goodsid":3,"goodsname":"vivo","goodsprice":"888","goodsimgpath":"imgs/shouji5.jpg"};
var goodsjson = JSON.stringify(data);
$.ajax({
url: "../test1/update.do",
data:goodsjson,
type:"POST",
datatype:"json",
contentType: "application/json",
success:function (response) {
alert(response.msg);
}
});
});
//删除商品信息
$("#but3").click(function () {
$.ajax({
url:"../test1/dalete.do?goodsid=4",
type:"GET",
dataType: "json",
success:function (response) {
alert(response.msg);
}
});
});
//根据ID查询商品信息
$("#but4").click(function () {
$.ajax({
url:"../test1/findOne.do?goodsid=2",
type:"GET",
datatype: "json",
success:function (response) {
alert(response.goodsname+","+response.goodsprice);
}
});
});
//查询所有商品信息
$("#but5").click(function () {
$.ajax({
url:"../test1/findList.do",
type:"GET",
dataType:"json",
success:function (response) {
for (var i=0;i<response.length;i++){
alert(response[i].goodsname+","+response[i].goodsprice);
}
}
});
});
});
</script>
</head>
<body>
<input id="but1" type="button" value="测试添加数据"/>
<input id="but2" type="button" value="测试修改数据"/>
<input id="but3" type="button" value="测试删除数据"/>
<input id="but4" type="button" value="测试根据id查询"/>
<input id="but5" type="button" value="测试查询所有"/>
</body>
</html>
十六、测试模板页面
1.在原有项目基础上创建控制器类
package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.GoodBean;
import com.wangxing.springboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test1")
public class TestController {
@Autowired
private GoodsService goodsService;
@RequestMapping("/findOnetest.do")
public String findOneGoods(int goodsid, Model model){
GoodBean goodBean = goodsService.selectGoodsById(goodsid);
model.addAttribute("goodBean",goodBean);
return "goodsitem";
}
}
注意:我们的方法访问路径是:/test1/findOnetest.do
2.创建商品页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>商品页面</title>
</head>
<body>
<a href="test1/findOnetest.do?goodsid=1"><img src="test1/imgs/shouji1.jpg"></a>
<a href="test1/findOnetest.do?goodsid=2"><img src="test1/imgs/shouji2.jpg"></a>
<a href="test1/findOnetest.do?goodsid=3"><img src="test1/imgs/shouji3.jpg"></a>
</body>
</html>
这里我们点击超链接后就会访问我们的请求处理方法,请求地址栏就会变成
所以我们的静态资源就创建成这样
让其在test1下面这样就可以访问到了