把 mybatis 整合到其中

pull/1/head
YunaiV 2019-02-22 00:01:06 +08:00
parent 85846516f1
commit aedecc44d1
10 changed files with 82 additions and 22 deletions

View File

@ -22,6 +22,19 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -1,13 +1,20 @@
package cn.iocoder.mall.product; package cn.iocoder.mall.product;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import javax.sql.DataSource;
@SpringBootApplication @SpringBootApplication
@MapperScan("cn.iocoder.mall.product.dao") // 扫描对应的 Mapper 接口
public class ProductRestApplication { public class ProductRestApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(ProductRestApplication.class, args); ConfigurableApplicationContext ctx = SpringApplication.run(ProductRestApplication.class, args);
DataSource ds = ctx.getBean(DataSource.class);
System.out.println(ds);
} }
} }

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.product.controller; package cn.iocoder.mall.product.controller.user;
import cn.iocoder.mall.product.vo.ProductCategoryVO; import cn.iocoder.mall.product.vo.ProductCategoryVO;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -10,7 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/product/category") @RequestMapping("user/product/category")
public class ProductCategoryController { public class ProductCategoryController {
// TODO 获得父编号为 id 的分类们 后面,使用 swagger 注释 // TODO 获得父编号为 id 的分类们 后面,使用 swagger 注释

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.product.controller; package cn.iocoder.mall.product.controller.user;
import cn.iocoder.mall.product.bo.ProductSpuBO; import cn.iocoder.mall.product.bo.ProductSpuBO;
import cn.iocoder.mall.product.service.ProductSpuService; import cn.iocoder.mall.product.service.ProductSpuService;
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/product/spu") @RequestMapping("user/product/spu")
public class ProductSpuController { public class ProductSpuController {
@Autowired @Autowired

View File

@ -1,15 +0,0 @@
package cn.iocoder.mall.product.dao;
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.springframework.stereotype.Repository;
@Repository
public class ProductSpuDAO {
public ProductSpuDO selectById(Integer id) {
ProductSpuDO spu = new ProductSpuDO();
spu.setId(id);
return spu;
}
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.mall.product.dao;
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductSpuMapper {
ProductSpuDO selectById(Integer id);
}

View File

@ -1,7 +1,7 @@
package cn.iocoder.mall.product.service; package cn.iocoder.mall.product.service;
import cn.iocoder.mall.product.bo.ProductSpuBO; import cn.iocoder.mall.product.bo.ProductSpuBO;
import cn.iocoder.mall.product.dao.ProductSpuDAO; import cn.iocoder.mall.product.dao.ProductSpuMapper;
import cn.iocoder.mall.product.dataobject.ProductSpuDO; import cn.iocoder.mall.product.dataobject.ProductSpuDO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -11,7 +11,7 @@ import org.springframework.stereotype.Service;
public class ProductSpuService implements cn.iocoder.mall.product.service.api.ProductSpuService { public class ProductSpuService implements cn.iocoder.mall.product.service.api.ProductSpuService {
@Autowired @Autowired
private ProductSpuDAO productSpuDAO; private ProductSpuMapper productSpuDAO;
public ProductSpuBO getProductSpu(Integer id) { public ProductSpuBO getProductSpu(Integer id) {
ProductSpuDO productSpuDO = productSpuDAO.selectById(id); ProductSpuDO productSpuDO = productSpuDAO.selectById(id);

View File

@ -0,0 +1,13 @@
spring:
# datasource
datasource:
url: jdbc:mysql://127.0.0.1:33061/mall_product?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
# mybatis
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: cn.iocoder.mall.product.dataobject

View File

@ -0,0 +1,12 @@
<?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="cn.iocoder.mall.product.dao.ProductSpuMapper">
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
SELECT
id
FROM product_spu
WHERE id = #{id}
</select>
</mapper>

View File

@ -0,0 +1,19 @@
<?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>
<settings>
<!-- 使用驼峰命名法转换字段。 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer"/>
<typeAlias alias="Long" type="java.lang.Long"/>
<typeAlias alias="HashMap" type="java.util.HashMap"/>
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
</typeAliases>
</configuration>