文章目录
1 建包controller,mapper,pojo,service,utils
2 创建配置文件 web.xml、applicationContext.xml、mybatis-config.xml
3 配置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">
<!--注册前端控制器DispatcherServlet-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--DispatcherServlet绑定spring配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--启动级别-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</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>
<!--超时时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
<!--首页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4 配置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>
<!--别名-->
<typeAliases>
<package name="com.cxj.pojo"/>
</typeAliases>
</configuration>
5 配置springmvc-servlet.xml
头文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
5.1 注解扫描
<!--注解扫描-->
<context:component-scan base-package="com.cxj.controller"/>
5.2 核心三要素 (handlerMapping,handlerAdapt,视图解析器)
<!--处理器映射器,处理器适配器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean id="InternalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--拼接路径 prefix前置路径 suffix后置路径-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
5.3 处理静态资源
<!--筛选静态资源 .css .jsp .html ....-->
<mvc:default-servlet-handler/>
6 配置spring-mapper.xml
头文件:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
6.1 注解扫描
<!--注解扫描,让包下的注解生效-->
<context:component-scan base-package="com.cxj.mapper"/>
6.2 配置数据源
<!--连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/数据库名称?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true" />
<property name="username" value="root" />
<property name="password" value="密码" />
<property name="maxActive" value="10" /><!--最大连接数-->
<property name="minIdle" value="2" /><!--最小连接数-->
<property name="maxWait" value="15000" /><!--最大等待时长-->
<property name="timeBetweenEvictionRunsMillis" value="60000" /><!--60秒运行一次空闲连接回收器-->
<property name="minEvictableIdleTimeMillis" value="180000" /><!--连接空闲30分钟被回收-->
<property name="removeAbandoned" value="true" /><!--启动连接泄露关闭功能-->
<property name="removeAbandonedTimeout" value="600" /><!--连接超过10分钟未关闭,强行回收连接-->
<property name="logAbandoned" value="true" /><!-- 关闭abanded连接时输出错误日志 -->
<property name="filters" value="stat" /><!-- 添加sql监控过滤器 -->
</bean>
6.3 sqlSessionFactory
<!--sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定mybatis的配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
6.4 动态扫描mapper
<!--mapper扫描包,动态的实现mapper接口注册到spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--要扫描的mapper包-->
<property name="basePackage" value="com.cxj.mapper"/>
</bean>
7 配置spring-service.xml
头文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
7.1 注解扫描
<!--注解扫描-->
<context:component-scan base-package="com.cxj.service"/>
7.2 配置声明式事务
<!--配置声明式事务 增删改事务自动提交-->
<bean id="transactionManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
注册bean
<!--set 方法-->
<bean id="GoodsServiceImpl" class="com.cxj.service.GoodsServiceImpl">
<property name="GoodsMapper" ref="goodsMapper"/>
</bean>
8.创建 mapper接口 还有 mapper.xml
创建接口:
public interface GoodsMapper {
/**
* 查询所有
*/
List<Goods> queryAll();
}
GoodsMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxj.mapper.GoodsMapper">
<select id="queryAll" resultType="goods">
select * from goods;
</select>
</mapper>
9.测试 mvc ,mybatis
测试类:
public class Application {
public static void main(String[] args) {
//ClassPathXmlApplicationContext拿到的bean必须是在xml中注册的
//如果是注解注册bean, 那你就要用AnnotationConfigApplicationContext 来映射
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
GoodsServiceImpl goodsServiceImpl = context.getBean("GoodsServiceImpl", GoodsServiceImpl.class);
for (Goods goods : goodsServiceImpl.queryAll()) {
System.out.println(goods);
}
}
}
测试结果: