本篇博客與其他大多數博客的不同之處在於,對依賴的添加進行了精簡,避免了依賴的不必要手動添加,提高開發效率
項目結構
導入依賴
pom.xml文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<!--spring web mvc依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--mybatis依賴-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--mysql依賴-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--spring整合mybatis依賴-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--spring管理數據源依賴-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--數據源依賴-->
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
</dependencies>
值得一提的是,注入spring-webmvc
依賴後,其他相關的spring依賴會自動導入,並不需要再手動添加,如spring-core
,spring-bean
等等
添加spring-webmvc後會自動導入這些依賴
配置文件
jdbc文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?characterEncoding=utf-8&useSSL=false
username=
password=
Web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置spring context配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicatioinContext.xml</param-value>
</context-param>
<!--配置DispatcherServlet-->
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext文件
需要注意的是,spring-servlet
跟spring-mybatis
只是spring上下文配置的子配置文件而已,因此在web.xml
中只需指定applicationContext
爲spring的上下文配置文件即可
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--導入spring配置文件-->
<import resource="classpath:spring/*.xml" />
</beans>
spring-servlet文件
<?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
https://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">
<!--Enable MVC Configuration-->
<mvc:annotation-driven/>
<!--Scan web component-->
<!--即掃描controller以及service註解等-->
<context:component-scan base-package="xyz.rosewuu"/>
<!--處理靜態文件的請求-->
<mvc:default-servlet-handler/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
spring-mybatis文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:mapper/*.xml" />
</bean>
<!--引入jdbc配置文件-->
<context:property-placeholder location="classpath:config/jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!--配置SqlSessionTemplate,獲取sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!--注入SqlSessionTemplate-->
<bean id="testService" class="xyz.rosewuu.service.impl.TestServiceImpl">
<property name="sqlSession" ref="sqlSession" />
</bean>
</beans>
數據庫結構
測試類
控制類
package xyz.rosewuu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.rosewuu.service.TestService;
import java.io.IOException;
@Controller
public class Test {
@Autowired
private TestService testService;
@RequestMapping("/test")
@ResponseBody
public String hello() throws IOException {
testService.hello();
return "Hello";
}
}
實體類
package xyz.rosewuu.entity;
public class Test {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Test{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Service層接口
package xyz.rosewuu.service;
import java.io.IOException;
public interface TestService {
void test() throws IOException;
}
Service層實現類
package xyz.rosewuu.service.impl;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import xyz.rosewuu.entity.Test;
import xyz.rosewuu.mapper.TestMapper;
import xyz.rosewuu.service.TestService;
import java.io.IOException;
@Service
public class TestServiceImpl implements TestService {
private SqlSession sqlSession;
public void setSqlSession(SqlSession sqlSession){
this.sqlSession = sqlSession;
}
@Override
public void test() throws IOException {
Test test = sqlSession.getMapper(TestMapper.class).test();
System.out.println(test);
}
}
Dao接口(Mapper)
package xyz.rosewuu.mapper;
import xyz.rosewuu.entity.Test;
public interface TestMapper {
Test test();
}
Mapper配置文件
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.rosewuu.mapper.TestMapper">
<select id="test" resultType="xyz.rosewuu.entity.Test">
select * from test
</select>
</mapper>
最終效果