您的位置:首页 > 编程语言 > Java开发

SpringBoot+Mybatis+MySQL实现简单的信息管理功能

2020-07-05 14:42 99 查看

在b站跟着狂神学习springboot的时候,边学边了解,根据所学整合了mybatis和springboot的知识点并完成了一个信息管理功能的小练习。在我的博客中记录下,顺便总结遇到的坑。
大致流程如下:

1.新建项目,导入依赖

在idea中新建一个springboot工程,除了springboot自带的依赖之外,我们还需要引入mysql依赖、jdbc依赖和mysql依赖。

<!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--jdbc和mysql-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

2.创建数据表

使用的数据库是mysql,表明及具体字段如下图。一共有员工表和部门表,员工表里有个外键did关联部门表的主键id,表示该员工所在的部门编号。

3.创建实体类pojo

分别创建员工类和部门类
员工类

public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department dept;
private Date birth;
}

部门类

public class Department {
private Integer id;
private String departmentName;
}

4.创建mapper文件夹

这个文件夹主要定义一些接口,接口中包含我们要对数据库进行持久化操作的方法。
EmployeeMapper

//得到所有员工
List<Employee> getAllEmployee();

//通过id得到员工
Employee getEmployeeById(int id);

//增
int insertEmp(Employee employee);

//删
int deleteEmp(int id);

//改
int updateEmp(Employee employee);

DepartmentMapper

//查询所有部门
public interface DepartmentMapper {
//查询所有部门
Collection<Department> queryDepartments();

//通过id得到部门
Department getDepartmentById(int id);
}

注意两个mapper接口上面都需要加上@Mapper和@Repository注
@Mapper注解的作用是给mapper接口生成一个实现类,从而让spring对mapper接口的bean进行管理。
以下是@Mapper的有关源码

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!AutoConfigurationPackages.has(this.beanFactory)) {
MybatisAutoConfiguration.logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.");
} else {
//这里开始查找有@Mapper注解的类
MybatisAutoConfiguration.logger.debug("Searching for mappers annotated with @Mapper");
//获取要扫描的路径
List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
if (MybatisAutoConfiguration.logger.isDebugEnabled()) {
packages.forEach((pkg) -> {
MybatisAutoConfiguration.logger.debug("Using auto-configuration base package '{}'", pkg);
});
}
//接下来应该就是注册bean的一些操作
//这个builder还不太清楚,可能是为了给扫描设置一些属性的一个东西
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class);
builder.addPropertyValue("processPropertyPlaceHolders", true);
//设置要扫描的类,也就是标注了@Mapper的class
builder.addPropertyValue("annotationClass", Mapper.class);
//设置要扫描的包,获取包里的信息
builder.addPropertyValue("basePackage", StringUtils.collectionToCommaDelimitedString(packages));
//这个也没见过,去查了一下应该是将上面的属性啥的信息包装成bean
BeanWrapper beanWrapper = new BeanWrapperImpl(MapperScannerConfigurer.class);
//过滤器
Stream.of(beanWrapper.getPropertyDescriptors()).filter((x) -> {
return x.getName().equals("lazyInitialization");
}).findAny().ifPresent((x) -> {
builder.addPropertyValue("lazyInitialization", "${mybatis.lazy-initialization:false}");
});
//注册bean
registry.registerBeanDefinition(MapperScannerConfigurer.class.getName(), builder.getBeanDefinition());
}
}

简单来说,它的作用是先根据@Mapper标注的对象获取需要扫描的路径,通过builder获取扫描包、类名等信息,再把它包装成一个bean,最后将这个bean进行注册。

@Repository
这个是一个spring注解,主要作用是标识当前类需要交给spring容器管理。其实这个不加也行,因为@Mapper注解已经帮我们把接口的代理类交给容器管理了。

5.创建xml映射文件

这里需要写一些对数据库进行具体的操作,比如增删改查啥的。这里需要注意的是,由于员工表的外键did关联了部门表的主键id。因此在查询的时候要用到联表查询,但在员工实体类中部门的属性定义是一个Department对象,因此在查询的时候需要用到结果集映射ResultMap来处理这个对象。
比如:

<select id="getAllEmployee" resultMap="empDept">
select e.id eid,e.lastName ename,e.email el,e.gender eg,d.departmentName dname,e.birth eb
from employee e,department d
where e.did=d.id;
</select>
<resultMap id="empDept" type="Employee">
<result property="id" column="eid"/>
<result property="lastName" column="ename"/>
<result property="email" column="el"/>
<result property="gender" column="eg"/>
<result property="birth" column="eb"/>
<association property="dept" javaType="Department">
<result property="departmentName" column="dname"/>
</association>
</resultMap>

6.创建controller层

controller负责业务模块流程的设计,在这里可以调用Mapper里接口的一些实现方法,从而获取数据传输给前端。整个页面实现大概是这样的(前端页面用的是学习视频里的模板(#.#))

7.登录设计

登录页面设计的比较简单,如果用户登录成功,就会登录的信息用

session.setAttribute()
记录下来,下次再登录用创建一个HandlerInterceptor拦截器去获取session的信息,如果取到的为空,则自动跳转到首页要求登录

github:https://github.com/qtlsd/springboot-study
刚开始学习阅读源码,还有很多不足之处希望能有大神能多给提提建议~~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: