DAO层
非配置文件:
1.mybatis动态代理接口
xxxmapper.java文件
2.mybatis动态接口的映射文件
xxxmapper.xml文件
3.数据库中的表
4.实体类
表对应的类
配置文件:
1.mybatis主配置文件,这个文件空的,因为跟spring整合,所以在spring中配置,不过要保存一个空的文件,要不然会报错
SqlMapConfig.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>
</configuration>
2.spring层配置文件
applicationContext-dao.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"
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">
<!-- 配置jdbc属性文件扫描器-->
<context:property-placeholder location="classpath:jdbc/jdbc.properties"/>
<!-- 配置一个数据库连接的对象 对象名字为dataSource,
这个就是德鲁伊包给的一个类,配置一个对象,所有连接数据库的属性都在里面设置,
然后可以用这个对象去连接数据库-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close" lazy-init="false">
<!-- 从属性文件获取连接数据库的参数-->
<!-- 其实就是给这个对象的属性赋值-->
<property name="driverClassName" value="${jdbc.drive}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 获取连接池的参数-->
<!-- 继续赋值,连接池属性的赋值-->
<property name="initialSize" value="${initialSize}"/>
<property name="minIdle" value="${minIdle}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxWait" value="${maxWait}"/>
</bean>
<!-- 创建一个工厂类的对象,来通过上面的数据库连接驱动操作数据库
这个本来是mybatis 的工作,用spring配置更方便一点-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置这个对象的属性 -->
<!-- 提供访问数据库的数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- 配置mybatis的核心主配置文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
<!-- 配置mybatis的别名包扫描器,这个用了之后.在mapper文件里面就可以使用别名,返回值可以使用,方便-->
<property name="typeAliasesPackage" value="com.xxxx.pojo"/>
</bean>
<!-- 配置mybatis的动态代理及映射位置-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这是需要映射的包的位置-->
<property name="basePackage" value="com.xxxx.mapper"/>
</bean>
</beans>
3:JDBC属性文件
jdbc.properties
jdbc.drive = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybd1?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2b8
jdbc.user = root
jdbc.password = root
## 设置连接池的参数
## 初始连接数
initialSize=10
## 最小空闲数
minIdle=10
## 最大连接数
maxActive=30
## 最大的等待时间
maxWait=6000
Service层
非配置文件
1.业务接口类
xxxservice.java
2.业务接口实现类
xxxserviceimpl.java
配置文件
applicationContext-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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 导入dao层文件,可以使用dataSource来陪着事务管理器-->
<import resource="classpath:spring/applicationContext-dao.xml"/>
<!-- 使用注解开发的包扫描器-->
<context:component-scan base-package="com.bjpwnd.service"/>
<!--事务管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 这个可以用注解来表明事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
使用注解管理jdbc事务
@Transactional(isolation = Isolation.SERIALIZABLE,propagation = Propagation.REQUIRED)
public boolean transfer(int out, int in, int money) {
int money1 = -money;
int i = accountMapper.changeMoney(out, money1);
// int i1 = 10 / 0;
int j = accountMapper.changeMoney(in, money);
return i > 0 && j > 0;
}
Controller层
非配置文件
1.控制器类
xxxcontroller.java
配置文件
springmvc.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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用注解开发的包扫描器-->
<context:component-scan base-package="com.bjpwnd.controller"/>
<!--配置处理器映射器-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>-->
<!--配置处理器适配器-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>-->
<!-- 使用默认的servlet来响应静态文件,如果在web.xml中定义了路径为 '/',会导致不能访问
静态资源,比如说加载图片之类的,如果配置了.xx就不会-->
<mvc:default-servlet-handler/>
<!-- 这个效果等同于上面 location表示路径 mapping表示匹配的文件-->
<!-- /xx/ /**表示所有文件-->
<!-- <mvc:resources mapping="" location=""/>-->
<!--可以配合@ResponseBody使用,还可以直接返回json对象 -->
<mvc:annotation-driven/>
<!-- 视图解析器配置:视图解析器 可能相当于之前的 servlet的增强-->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 初始化spring容器文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<!-- 用监听器加载所有的spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置servlet映射,跟之前的servlet差不多 不过多了一个init-param-->
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 容器配置文件路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!--会解析所有以.go结尾的访问-->
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>*.go</url-pattern>
</servlet-mapping>
<!-- 过滤器,改编码用-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
项目文件结构
image.png