项目结构
配置文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< groupId> com.hzx</ groupId>
< artifactId> day20200819</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< packaging> war</ packaging>
< dependencies>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-webmvc</ artifactId>
< version> 5.1.6.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.1.6.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 5.1.6.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.1.6.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 5.1.6.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis-spring</ artifactId>
< version> 1.3.1</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> fastjson</ artifactId>
< version> 1.2.54</ version>
</ dependency>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.4.6</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.47</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.16</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.12</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 3.1.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
< scope> test</ scope>
</ dependency>
</ dependencies>
</ project>
spring-context.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: tx= " http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: property-placeholder location = " classpath:db.properties" />
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" init-method = " init" destroy-method = " close" >
< property name = " driverClassName" value = " ${driver}" />
< property name = " url" value = " ${url}" />
< property name = " username" value = " ${name}" />
< property name = " password" value = " ${password}" />
</ bean>
< bean id = " sqlSessionFactory" class = " org.mybatis.spring.SqlSessionFactoryBean" >
< property name = " dataSource" ref = " dataSource" />
< property name = " mapperLocations" value = " classpath:mapper/*-mapper.xml" />
< property name = " typeAliasesPackage" value = " com.entity" />
< property name = " configLocation" value = " classpath:mybatis-config.xml" />
</ bean>
< bean class = " org.mybatis.spring.mapper.MapperScannerConfigurer" >
< property name = " basePackage" value = " com.dao" />
</ bean>
< bean id = " txManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" />
</ bean>
< context: component-scan base-package = " com" use-default-filters = " true" >
< context: exclude-filter type = " annotation" expression = " org.springframework.stereotype.Controller" />
</ context: component-scan>
< tx: annotation-driven transaction-manager = " txManager" />
</ beans>
mybatis-config.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>
< settings>
< setting name = " cacheEnabled" value = " true" />
< setting name = " mapUnderscoreToCamelCase" value = " true" />
</ settings>
</ configuration>
mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: context= " http://www.springframework.org/schema/context"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
< context: component-scan base-package = " java" use-default-filters = " false" >
< context: include-filter type = " annotation" expression = " org.springframework.stereotype.Controller" />
</ context: component-scan>
< mvc: annotation-driven/>
< bean class = " org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = " prefix" value = " /" />
< property name = " suffix" value = " .jsp" />
</ bean>
< mvc: default-servlet-handler/>
</ beans>
/WEB-INF/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" >
< servlet>
< servlet-name> mvc</ servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class>
< init-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:mvc.xml</ param-value>
</ init-param>
< load-on-startup> 1</ load-on-startup>
</ servlet>
< servlet-mapping>
< servlet-name> mvc</ servlet-name>
< url-pattern> /</ url-pattern>
</ servlet-mapping>
< filter>
< filter-name> encoding</ 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> encoding</ filter-name>
< url-pattern> /*</ url-pattern>
</ filter-mapping>
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener</ listener-class>
</ listener>
< context-param>
< param-name> contextConfigLocation</ param-name>
< param-value> classpath:spring-context.xml</ param-value>
</ context-param>
</ web-app>
entity
Department.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
private Integer id;
private String name;
private String location;
@DateTimeFormat ( pattern = "yyyy-MM-dd" )
private Date createTime;
}
dao.DepartmentDAO.java
public interface DepartmentDAO {
Department queryDepartmentById ( @Param ( "id" ) Integer id) ;
}
service
DepartmentService.java
public interface DepartmentService {
Department queryDepartmentById ( Integer id) ;
}
DepartmentServiceImpl.java
@Setter
@Service ( "deptService" )
@Transactional ( isolation = Isolation. DEFAULT, propagation = Propagation. REQUIRED, timeout = - 1 , readOnly = false , rollbackFor = Exception. class )
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentDAO departmentDAO;
@Transactional ( propagation = Propagation. SUPPORTS)
public Department queryDepartmentById ( Integer id) {
return departmentDAO. queryDepartmentById ( id) ;
}
}
controller/DepartmentController.java
@Controller
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@GetMapping ( "/test1" )
public String queryDept ( ) {
System. out. println ( departmentService. queryDepartmentById ( 1 ) ) ;
return "department" ;
}
}
resource/mapper/derpartmentdao-mapper.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.dao.DepartmentDAO" >
< select id = " queryDepartmentById" resultType = " Department" >
select id,name,location,create_time
from department
where id=#{id}
</ select>
</ mapper>
测试
DAOTest.java
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( "classpath:spring-context.xml" )
public class DAOTest {
@Autowired
private DepartmentDAO departmentDAO;
@Test
public void test1 ( ) {
System. out. println ( departmentDAO. queryDepartmentById ( 1 ) ) ;
}
}
ServiceTest.java
@RunWith ( SpringJUnit4ClassRunner. class )
@ContextConfiguration ( "classpath:spring-context.xml" )
public class ServiceTest {
@Autowired
private DepartmentService departmentService;
@Test
public void test1 ( ) {
System. out. println ( departmentService. queryDepartmentById ( 1 ) ) ;
}
}