springboot-3-2:数据库操作之代码详细描述

1. com.ais.bootstrap.controller.TestController.java

@RestController是@ResponseBody + @Controller:所以可用于返回JSON结构数据。

@Autowired 注入:此处的用法是调用class TestService中的代码(实际调用了interface TestService)

@RestController
@RequestMapping("/test/")
public class TestController {
    @Autowired
    private TestService testService;

    /**
     * 获取所有的区域信息
     *
     * @return
     */
    @RequestMapping(value = "/listarea", method = RequestMethod.GET)
    private Map<String, Object> listArea() {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<Test> list = new ArrayList<Test>();


        // 获取区域列表
        list = testService.XgwTest();
        modelMap.put("areaList", list);
        return modelMap;
    }

}

2. com.ais.bootstrap.service.TestService.java

这是一种interface的写法,屏蔽后端业务逻辑

public interface TestService {

    List<Test> getTest();

    /**
     * 根据Id列出具体区域
     *
     * @return area
     */
    Test geTestById(int id);

3. com.ais.bootstrap.service.impl.TestServiceImpl.java

实际业务逻辑,interface使用implements做实现(允许多态)

3.1.@Service表示被注册为一个业务层组件

3.2.@Autowired 注入 TestDao这个类(实际用的是xml封装的Dao层对象)

3.3 @Override 表示对 在2中的interface定义的方法的重写(限制必须有方法且方法的参数类型一致)

@Service
//@Transactional
public class TestServiceImpl implements TestService {
    @Autowired
    private TestDao testDao;

    @Override
    public List<Test> getTest() {
        // 返回所有的区域信息
        return testDao.queryTest();
    }

    @Override
    public Test getTestById(int id) {
        return testDao.queryTestbyId(id);
    }

    @Transactional
    @Override
    public boolean addTest(Test test) {
        // 空值判断,主要是判断areaName不为空
        if (test.getBeginTs() != null && !"".equals(test.getDstGroup())) {
            // 设置默认值
            test.setGmtCreate(new Date());
            try {
                int effectedNum = testDao.insertTest(test);
                if (effectedNum > 0) {
                    return true;
                } else {
                    throw new RuntimeException("添加区域信息失败!");
                }
            } catch (Exception e) {
                throw new RuntimeException("添加区域信息失败:" + e.toString());
            }
        } else {
            throw new RuntimeException("区域信息不能为空!");
        }
    }

}

4.com.ais.bootstrap.dao.TestDao.java

这个Dao层也是一个interface,但不是使用implements做具体实现,而是用mybatis做具体实现

public interface TestDao {

    List<Test> queryTest();

    /**
     * 根据Id列出具体区域
     *
     * @return area
     */
    Test queryTestbyId(int id);

    /**
     * 插入区域信息
     *
     * @param test
     * @return
     */
    int insertTest(Test test);

    /**
     * 更新区域信息
     *
     * @param test
     * @return
     */

5.resource.mybatis.mapper.TestDao.xml

该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.ais.bootstrap.dao.TestDao">
  <select id="queryTest" resultType="com.ais.bootstrap.entity.Test">
		SELECT id,gmt_create,begin_ts,dst_group FROM _test
	</select>
  <select id="queryTestbyId" resultType="com.ais.bootstrap.entity.Test">
		SELECT id,gmt_create,begin_ts,dst_group FROM _test
		WHERE
		id=#{id}
	</select>
  <insert id="insertTest" useGeneratedKeys="true" keyProperty="id"
          keyColumn="id" parameterType="com.ais.bootstrap.entity.Test">
		INSERT INTO
		_test(gmt_create,begin_ts,dst_group)
		VALUES
		(#{GmtCreate},
		#{BeginTs},#{DstGroup})
	</insert>
</mapper>

6. Mapper文件的定义和依赖:Mybatis-config.xml

Mybatis-config.xml这个文件的路径可在application.properties中配置。但这个文件中会配置对上述xml的调用和注册。

<?xml version="1.0" encoding="UTF-8" ?>
<!-- mybatis的配置文件 -->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="useColumnLabel" value="true" />
        <setting name="mapUnderscoreToCamelCase" value="true" />
        </settings>
	<mappers>
        <mapper resource="mybatis/mapper/xgwTestDao.xml"/>
    </mappers>
</configuration>

总结:通过上述六个文件:一个原始SQL被逐层调用并封装为了可被前端读取的RestController接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值