思路
将dao层和service作为bean放入全局bean(spring-application.xml),在service层中调用dao层方法,测试直接调用全局bean,直接调用service
maven项目目录结构
pro.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!--J2EE-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mysql驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- Fastjoson JSON处理工具 --><!-- TODO:gai -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<!-- 文件上传下载-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- 数据校验 -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.1.0.CR2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.0.Final</version>
</dependency>
<!-- @ResponesBody的新解决方案-jackson包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- Swagger2 - fasterxml -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.5.1</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 阿里的druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
<scope>compile</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!-- github分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.2.1</version>
</dependency>
jdbc.properties
#url
driver=com.mysql.cj.jdbc.Driver
#mysqlµÄjdbcÁ¬½ÓµØÖ·
url=jdbc:mysql://localhost:3308/school?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
#mysqlÊý¾Ý¿âµÄÓû§Ãû
user=root
#mysqlÊý¾Ý¿âµÄÃÜÂë
pwd=root
mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--加载配置-->
<!--
ignore-unresolvable为true时,配置文件${}找不到对应占位符的值,直接赋值'${}'
-->
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
<!--配置数据源druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--driverClassName可以不写,智能匹配-->
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${pwd}" />
</bean>
<!--整合MyBatis-->
<!-- 配置 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--衔接数据源,目前使用的是数据库连接池-->
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
<!-- 配置Spring整合MyBatis,扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注册MyBatis的映射文件-->
<property name="basePackage" value="www.it.cast.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--使用sqlSessionTemplate 将接口和实现的xml相对应-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
spring-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--加载配置-->
<!--
ignore-unresolvable为true时,配置文件${}找不到对应占位符的值,直接赋值'${}'
-->
<context:component-scan base-package="www.it.cast.service"/>
</beans>
spring-application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--导入bean-->
<import resource="mybatis.xml"/>
<import resource="spring-service.xml"/>
</beans>
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="www.it.cast.dao.StudentMapper">
<!-- 这里的id是我们之前在接口定义的方法名, resultType是指返回结果的类型我们查询的是用户数据所以返回接口是User -->
<resultMap id="ResultMap" type="www.it.cast.entity.Student">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="student_name" jdbcType="VARCHAR" property="studentName"/>
<result column="class_id" jdbcType="VARCHAR" property="classId"/>
<result column="teacher_id" jdbcType="INTEGER" property="teacherId"/>
</resultMap>
<select id="getStudents" resultMap="ResultMap">
select * from student
</select>
<!-- 根据id删除方法-->
<delete id="delStudents" parameterType="java.lang.Integer">
delete from student where id = #{id}
</delete>
<!-- 修改方法-->
<update id="updStudents" parameterType="www.it.cast.entity.Student">
update student set student_name = #{studentName},class_id = #{classId},teacher_id=#{teacherId} where id = #{id};
</update>
<!-- 添加方法-->
<insert id="addStudents" parameterType="www.it.cast.entity.Student">
insert into student (student_name,class_id,teacher_id) values (#{studentName},#{classId},#{teacherId})
</insert>
</mapper>
实体类
package www.it.cast.entity;
public class Student {
private Integer id;
private String studentName;
private String classId;
private Integer teacherId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", studentName='" + studentName + '\'' +
", classId='" + classId + '\'' +
", teacherId=" + teacherId +
'}';
}
public Student(Integer id, String studentName, String classId, Integer teacherId) {
this.id = id;
this.studentName = studentName;
this.classId = classId;
this.teacherId = teacherId;
}
public Student(String studentName, String classId, Integer teacherId) {
this.studentName = studentName;
this.classId = classId;
this.teacherId = teacherId;
}
}
dao层
package www.it.cast.dao;
import org.mapstruct.Mapper;
import www.it.cast.entity.Student;
import java.util.List;
@Mapper
public interface StudentMapper {
//查询所有
List<Student> getStudents();
//删除方法
int delStudents(Integer id);
//修改方法
int updStudents(Student student);
//添加方法
int addStudents(Student student);
}
service层
package www.it.cast.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.it.cast.dao.StudentMapper;
import www.it.cast.entity.Student;
import java.util.List;
@Service(value = "studentService")
public class StudentService {
@Autowired
private StudentMapper studentMapper;
public List<Student> getStudent() {
return studentMapper.getStudents();
}
public String getStudentName() {
return "张三";
}
}
看累了,听首音乐放松一刻
评论区