文档:http://www.macrozheng.com/#/foreword/mall_foreword_01
一、框架:
1.1、pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
<!-- 配置不过滤配置文件,以免找不到对应配置文件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
1.2、pojo实体类:
根据数据库,写实体类(属性、构造方法、setter getter,toString方法)(代码略)
1.3、mapper层(mybatis,对数据库的操作)
1.3.1、mybatis-config.xml
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mall"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/ld/test/demo1/mapper/pmsBrand.xml"/>
</mappers>
</configuration>
1.3.2、mapper接口(作用:对数据库的增删改查接口)
package com.ld.test.demo1.mapper;
import com.ld.test.demo1.pojo.PmsBrand;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public interface PmsBrandMapper {
List<PmsBrand> getPmsBrandAll();
PmsBrand getPmsBrandById(int id);
PmsBrand selectPmsBrandByIF(@Param("id") int id, @Param("first_letter") String first_letter);
PmsBrand selectPmsBrandByIF2(Map<String,Object> map);
int addPmsBrand(PmsBrand pmsBrand);
int updatePmsBrand(PmsBrand pmsBrand);
}
1.3.3、mapper接口的实现(对应mapper的xml)PmsBrandMapper.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.ld.test.demo1.mapper.PmsBrandMapper">
<select id="getPmsBrandAll" resultType="com.ld.test.demo1.pojo.PmsBrand">
select * from pms_brand;
</select>
<!---->
<select id="getPmsBrandById" resultType="com.ld.test.demo1.pojo.PmsBrand">
select * from pms_brand where id=#{id};
</select>
<select id="selectPmsBrandByIF" resultType="com.ld.test.demo1.pojo.PmsBrand">
select * from pms_brand where id=#{id} and first_letter = #{first_letter};
</select>
<select id="selectPmsBrandByIF2" parameterType="map" resultType="com.ld.test.demo1.pojo.PmsBrand">
select * from pms_brand where id=#{id} and first_letter = #{first_letter};
</select>
<insert id="addPmsBrand" parameterType="com.ld.test.demo1.pojo.PmsBrand">
insert into pms_brand (name,first_letter,sort) values (#{name},#{first_letter},#{sort});
</insert>
</mapper>
1.4、Service层
1.4.1、service层接口:
package com.ld.test.demo1.service;
import com.ld.test.demo1.pojo.PmsBrand;
import java.util.List;
public interface PmsBrandService {
List<PmsBrand> listAllBrand();
int createBrand(PmsBrand pmsBrand);
int updateBrand(Long id, PmsBrand pmsBrand);
int deleteBrand(Long id);
List<PmsBrand> listBrand(int pageNum, int pageSize);
PmsBrand getBrand(Long id);
}
1.4.2、service层实现(调用mapper逻辑)
package com.ld.test.demo1.service.impl;
import com.ld.test.demo1.mapper.PmsBrandMapper;
import com.ld.test.demo1.pojo.PmsBrand;
import com.ld.test.demo1.service.PmsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
@Autowired
private PmsBrandMapper brandMapper;
@Override
public List<PmsBrand> listAllBrand() {
return brandMapper.getPmsBrandAll();
}
# ......
}
1.5、controller层,最外层接口的定义,调用service层
package com.ld.test.demo1.controller;
import com.ld.test.demo1.common.api.CommonResult;
import com.ld.test.demo1.pojo.PmsBrand;
import com.ld.test.demo1.service.PmsBrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/brand")
public class PmsBrandController {
private PmsBrandService demoService;
private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);
@RequestMapping(value = "listAll",method = RequestMethod.GET)
@ResponseBody
public CommonResult<List<PmsBrand>> getBrandList(){
return CommonResult.success(demoService.listAllBrand());
}
}