分享一篇前后端分离的简单的SpringBoot的项目实战示例(二)

1、说明

继上次分享之后,因为还有种编码格式事利用.XML文件去编码数据库的操作,所以现在分享一种利用.XML和.properties的方式去构建SpringBoot的方式。

2、重点

1、结构包的分享
在这里插入图片描述
2、各个层级的含义
已在上篇文章中有写,请查看
3、话不多说,直接上代码
1、studentController.java

package com.example.controller;


import com.example.entity.Student;
import com.example.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller  // 在Controller层必须要有的注解
@RequestMapping
public class studentController {

    @Autowired
    private studentService studentservice;

    /**
     * 查找全部
     */
    @RequestMapping("/listAll")
    public void listAll() {
        // 查找列表数据
        List<Student> students = studentservice.SelectAll();
        // 列出查找出的信息
        System.out.println(students);
    }

    /**
     * 按照性别查找所有信息
     *
     * @param sex
     * @return
     */
    @RequestMapping("/listBySex")
    public List<Student> listBySex(boolean sex) {
        List<Student> students1 = studentservice.SelectBySex(sex);
        if (students1.toString().equals("张飞小三")) {
            return null;
        }
        return students1;
    }
}

2、studentDao.java

package com.example.dao;


import com.example.entity.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
/**
 * @Description: TODO
 * @Data: 2021-12-29 15:26:44
 * @Pacakge: com.example.dao
 * @ClassName: studentDao
 * @Version: v1.0.0
 * @Author: Tao
 * @Remarks: 数据访问接口  持久层,接口层,和数据库互动,编程对数据库的操作的方法。
 **/

@Mapper
public interface studentDao {

    /**
     * 查找所有信息
     *
     * @return
     */
    List<Student> SelectAll();

    /**
     * 按照性别查找所有信息
     *
     * @return
     */
    List<Student> SelectBySex(Boolean sex);

    /**
     * 插入新的数据
     *
     * @return
     */
    String Insert();

}

3、Student.java

package com.example.entity;

import lombok.*;

import java.util.Date;

@Data   // Set设置  Get获取
@AllArgsConstructor  // 生成一个包含所有参数的构造方法
@NoArgsConstructor  // 生成一个无参数的构造方法
// @Repository(value = "/Student")
public class Student {

    // 学生姓名 数据库设置成varchar类型
    private String name;

    // 学生年龄 数据库设置成int类型
    private int age;

    // 学生性别 0(男) 、 1(女) 数据库设置成 tinyint类型
    private boolean sex;

    // 学生身份证号   数据库设置成 int类型,但明显长度不够
    private Integer IDCord;

    // 学生生日  数据库设置成Date类型
    private Date birthDay;
}

4、studentService.java

package com.example.service;

import com.example.entity.Student;

import java.util.List;

public interface studentService {

    List<Student> SelectAll();

    List<Student> SelectBySex(Boolean sex);

    String Insert();
}

5、studentServiceImpl.java

package com.example.service.serviceImpl;

import com.example.dao.studentDao;
import com.example.entity.Student;
import com.example.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("studentService")  // 加上该注解,将会将该类自动注入到spring容器中,不需要在applicationContext.xml中文件中再次定义bean了
@Transactional  // 需要AOP拦截及事务的处理,会影响到性能,只有对public才会起作用。放在实现接口的实现类上
public class studentServiceImpl implements studentService {

    /*注入dao层的方法*/
    @Autowired
    private studentDao studentdao;


    @Override
    public List<Student> SelectAll() {
        return studentdao.SelectAll();
    }

    @Override
    public List<Student> SelectBySex(Boolean sex) {
        return studentdao.SelectBySex(sex);
    }

    @Override
    public String Insert() {
        return studentdao.Insert();
    }
}

6、studentMapper.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.dao.studentDao">

    <resultMap id="studentmap" type="com.example.entity.Student">
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="sex" column="sex"/>
        <result property="IDCord" column="IDCord"/>
        <result property="birthDay" column="birthDay"/>
    </resultMap>

    <select id="SelectAll" resultType="com.example.entity.Student">
        SELECT * from student ORDER BY birthDay;
    </select>
    <select id="SelectBySex" resultType="com.example.entity.Student">
        SELECT * from student where sex = #{sex};
    </select>
    <insert id="Insert">
        INSERT INTO student(name,age,sex,IDCord,birthDay) values (#{name},#{age},#{sex},#{IDCord},#{birthDay});
    </insert>
</mapper>

7、application.properties

#Mybatis 配置 >>> 配置xml文件所在的路径,配置映射类所在的包名
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.dao
# 注解链接数据库
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=******
spring.datasource.password=*******
spring.datasource.dbcp2.max-idle=20
spring.datasource.dbcp2.min-idle=10
# 注解使tomcat端口号不冲突
server.port=8888

3、有任何问题请联系或留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值