提交 11c56917 编写于 作者: S studyingpanda

chen

上级 abb3391e
......@@ -7,3 +7,5 @@ spring.datasource.username=root
# 配置数据库密码
spring.datasource.password=Easy@0122
# 指定MyBatis配置文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
\ No newline at end of file
<?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"
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<version>2.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.imooc</groupId>
<artifactId>spring-boot-mybatis</artifactId>
......@@ -23,7 +24,39 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- myql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......
package com.imooc.springbootmybatis;
import java.util.List;
import org.springframework.stereotype.Repository;
/**
* 商品数据库访问接口
*/
@Repository // 标注数据访问组件
public interface GoodsDao {
/**
* 新增商品
*/
public int insert(GoodsDo Goods);
/**
* 删除商品(根据id)
*/
public int delete(Long id);
/**
* 修改商品信息(根据id修改其他属性值)
*/
public int update(GoodsDo Goods);
/**
* 查询商品信息(根据id查询单个商品信息)
*/
public GoodsDo selectOne(Long id);
/**
* 查询商品列表
*/
public List<GoodsDo> selectAll();
}
package com.imooc.springbootmybatis;
/**
* 商品类
*/
public class GoodsDo {
/**
* 商品id
*/
private Long id;
/**
* 商品名称
*/
private String name;
/**
* 商品价格
*/
private String price;
/**
* 商品图片
*/
private String pic;
// 省略 get set方法
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
}
package com.imooc.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.imooc.springbootmybatis") // 指定MyBatis扫描的包,以便将数据访问接口注册为bean
public class SpringBootMybatisApplication {
public static void main(String[] args) {
......
# 配置数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 配置数据库url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shop?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
# 配置数据库用户名
spring.datasource.username=root
# 配置数据库密码
spring.datasource.password=Easy@0122
mybatis.mapper-locations=classpath:mapper/*.xml
\ No newline at end of file
<?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">
<!-- 本映射文件对应GoodsDao接口 -->
<mapper namespace="com.imooc.springbootmybatis.GoodsDao">
<!-- 对应GoodsDao中的insert方法 -->
<insert id="insert" parameterType="com.imooc.springbootmybatis.GoodsDo">
insert into goods (name,price,pic) values (#{name},#{price},#{pic})
</insert>
<!-- 对应GoodsDao中的delete方法 -->
<delete id="delete" parameterType="java.lang.Long">
delete from goods where id=#{id}
</delete>
<!-- 对应GoodsDao中的update方法 -->
<update id="update" parameterType="com.imooc.springbootmybatis.GoodsDo">
update goods set name=#{name},price=#{price},pic=#{pic} where id=#{id}
</update>
<!-- 对应GoodsDao中的selectOne方法 -->
<select id="selectOne" resultMap="resultMapBase" parameterType="java.lang.Long">
select <include refid="sqlBase" /> from goods where id = #{id}
</select>
<!-- 对应GoodsDao中的selectAll方法 -->
<select id="selectAll" resultMap="resultMapBase">
select <include refid="sqlBase" /> from goods
</select>
<!-- 可服用的sql模板 -->
<sql id="sqlBase">
id,name,price,pic
</sql>
<!-- 保存SQL语句查询结果与实体类属性的映射 -->
<resultMap id="resultMapBase" type="com.imooc.springbootmybatis.GoodsDo">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="price" property="price" />
<result column="pic" property="pic" />
</resultMap>
</mapper>
\ No newline at end of file
package com.imooc.springbootmybatis;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.FixMethodOrder;
import org.junit.jupiter.api.Test;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* GoodsDao测试类
*/
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING) // 按方法名称顺序测试
class GoodsDaoTest {
@Autowired
private GoodsDao goodsDao;
/**
* 新增一个商品
*/
@Test
void test_01() {
GoodsDo goods = new GoodsDo();
goods.setName("手机");
goods.setPic("phone.jpg");
goods.setPrice("2000");
int count = goodsDao.insert(goods);
assertEquals(1, count);// count值为1则测试通过
}
/**
* 更新商品信息
*/
@Test
void test_02() {
GoodsDo goods = new GoodsDo();
goods.setId(1L);
goods.setName("手机");
goods.setPic("phone.jpg");
goods.setPrice("3000");
int count = goodsDao.update(goods);
assertEquals(1, count);// count值为1则测试通过
}
/**
* 获取商品信息
*/
@Test
void test_03() {
GoodsDo goods = goodsDao.selectOne(1L);
assertNotNull(goods);// goods不为null则测试通过
}
/**
* 删除商品
*/
@Test
void test_04() {
int count = goodsDao.delete(1L);
assertEquals(1, count);// count值为1则测试通过
}
/**
* 获取商品信息列表
*/
@Test
void test_05() {
List<GoodsDo> goodsList = goodsDao.selectAll();
assertEquals(0, goodsList.size());// goodsList.size()值为0则测试通过
}
}
package com.imooc.springbootmybatis;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootMybatisApplicationTests {
@Test
void contextLoads() {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册