本篇博客与其他大多数博客的不同之处在于,对依赖的添加进行了精简,避免了依赖的不必要手动添加,提高开发效率
如果你使用spring boot
的话,以下纯属瞎折腾,敬请跳过。
项目结构
导入依赖
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 + '\'' +
'}';
}
}
但本人并不建议实体类自己手动添加getter/setter等方法,因为一旦修改变量则工作变得繁琐。因此我推荐在IDEA中安装Lombok插件,具体可参考这里https://www.zhihu.com/question/42348457
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>
从mapper到mapper配置文件语句生成的这个过程,其实也是可以减少自己的工作量,那就是使用MybatisX插件,具体安装过程自己可快速搜索一下。
最终效果