首先导入springboot项目
项目结构图如下
1.下载依赖,在pom.xml文件添加如下代码
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
接下来便是application.yml文件内容的修改,代码如下,数据库的连接自己修改
server:
port: 8080
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shangchen?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis:
mapper-locations: classpath:mybatis/*Mapper.xml
在mybatis文件夹内填写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.example.first.dao.GoodsDao">
<select id="get" resultType="com.example.first.domain.GoodsDo">
select `gid`,`gname`,`gprice`,`gdesc` from Goods where gid = #{value}
</select>
<select id="list" resultType="com.example.first.domain.GoodsDo">
select
`gid`,`gname`,`gprice`,`gdesc`
from Goods
</select>
<insert id="save" parameterType="com.example.first.domain.GoodsDo" useGeneratedKeys="true" keyProperty="gid">
insert into Goods
(
`gid`, `gname`,`gprice`,`gdesc`
)
values
(
#{gid},#{gname},#{gprice},#{gdesc}
)
</insert>
<delete id="remove">
delete from Goods where gid = #{value}
</delete>
<update id="update">
update Goods set gprice = #{gprice} where gid = #{gid}
</update>
</mapper>
控制层代码
@RestController
public class IndexController {
@Autowired
private GoodsServer goodsServer;
@RequestMapping("/get")
public String get(int id) {
GoodsDo goodDo=goodsServer.get(id);
//System.out.println(goodDo);
return goodDo.toString();
}
@RequestMapping("/list")
public List<GoodsDo> list() {
List<GoodsDo> goodsDo=goodsServer.list();
return goodsDo;
}
@RequestMapping("/delete")
public String deldete(int id) {
goodsServer.remove(id);
return "it's ok!";
}
@RequestMapping("/update")
public String update(int id,double price) {
goodsServer.update(id,price);
return "it's ok!";
}
@RequestMapping("/insert")
public String insert(int id,String desc,String name,double price) {
GoodsDo goodsDo=new GoodsDo();
goodsDo.setGid(id);
goodsDo.setGdesc(desc);
goodsDo.setGname(name);
goodsDo.setGprice(price);
goodsServer.save(goodsDo);
return "it's ok!";
}
}
服务层goodsserver代码
public interface GoodsServer {
GoodsDo get(int id);
List<GoodsDo> list();
void save(GoodsDo GoodsContent);
void update(@Param("gid") int gid, @Param("gprice") double gprice);
int remove(int id);
}
服务层goodsserverimpl代码
@Service
@ComponentScan
public class GoodsServerImpl implements GoodsServer {
@Autowired
private GoodsDao goodsDao;
@Override
public GoodsDo get(int id){
return goodsDao.get(id);
}
@Override
public void update(int id,double gprice){
goodsDao.update(id,gprice); }
@Override
public int remove(int id){
goodsDao.remove(id);
return 1; }
@Override
public void save(GoodsDo goodsDo){
goodsDao.save(goodsDo); }
@Override
public List<GoodsDo> list(){
List<GoodsDo> goodsDo=goodsDao.list();
System.out.println(goodsDo);
return goodsDo; }
}
dao层googsDao代码
@Mapper
public interface GoodsDao {
GoodsDo get(int id);
List<GoodsDo> list();
int save(GoodsDo GoodsContent);
void update(@Param("gid") int gid, @Param("gprice") double gprice);
int remove(int id);
}
Do层goodsDo代码
@Getter
@Setter
public class GoodsDo {
private int gid;
private String gname;
private double gprice;
private String gdesc;
public String toString() {
return "{gid='" + this.gid + '\'' + ", gname='" + this.gname + '\'' + ", gprice='" + this.gprice + '\'' + ", gdesc='" + this.gdesc +'}';
}
}
启动类,记得写上@MapperScan(“com.example.first.dao”)
@SpringBootApplication
@MapperScan("com.example.first.dao")
public class FirstApplication {
//将springboot应用启动
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
运行结果截图如下
好了,springboot的代码讲解就到这了,对于新手来说,这个简单的项目可以帮助你们更好的理解springboot框架。