- 导入jar包
- 配置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的-->
<!-- 两个SpringMVC的-->
<!-- 两个Spring的-->
<!-- 1.上下文参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 2.监听器-->
<!-- 作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 两个SpringMVC的-->
<!-- 1.Springmvc前端控制器-->
<servlet>
<servlet-name>springmvc</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>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 2.字符编码过滤器-->
<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>
</web-app>
- 配置src下全局配置文件
3.1 Spring的
3.2 SpringMVC的
Spring的配置文件
1.注解扫描
2.加载属性文件
3.数据源
4.SqlSessionFactory
5.扫描器
6.事务管理器
7.声明式事务
8.配置aop
<?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:aop="http://www.springframework.org/schema/aop"
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/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" default-autowire="byName">
<!-- 注解扫描-->
<context:component-scan base-package="com.steer.service.impl"></context:component-scan>
<!-- 加载属性文件-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- SqlsessionFactory-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 给包下的类取别名-->
<property name="typeAliasesPackage" value="com.steer.pojo"></property>
</bean>
<!-- 扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.steer.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!--事务管理器-->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明式事务-->
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<!-- 哪些方法需要有事务控制-->
<!-- 方法以ins 开头事务管理-->
<tx:method name="ins*"/>
<tx:method name="upd*"/>
<tx:method name="del*"/>
<!-- readonly=”boolean” 是否是只读事务.-->
<!-- 如果为true,告诉数据库此事务为只读事务.数据化优化,会对性能有一定提升,所以只要是查询的方法,建议使用此数据.-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop-->
<aop:config>
<!-- 切点为impl中所有类的所有方法,带任何参数的-->
<aop:pointcut id="mypoint" expression="execution(* com.steer.service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
SpringMVC的配置文件
1.扫描注解
2.注解驱动
3.设置静态资源
4.视图解析器
<?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:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解。只扫描controller包-->
<context:component-scan base-package="com.steer.controller"></context:component-scan>
<!-- 注解驱动。注册HandlerMapping 和 HandlerAdapter-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置静态资源-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
<mvc:resources mapping="/files/**" location="/files/"></mvc:resources>
<!-- 视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 写pojo实体类
- 写mapper
- 写service
- 写impl
加Service,将该类交给容器Spring管理
@Service
public class MenuServiceImpl implements MenuService {
// 声明Mapper 接口对象,并生成get/set 方法
// 使用该注解,可以省略get/set方法
@Resource
private MenuMapper menuMapper;
@Override
public List<Menu> show() {
return menuMapper.selByPid(0);
}
}
- 写controller
@Controller 表示将当前类交给springmvc容器进行管理
// @Controller 表示将当前类交给springmvc容器进行管理
@Controller
public class MenuController {
/**
* springmvc容器调用Spring容器中的内容
*/
@Resource
private MenuService menuServiceImpl; // 通过byName去寻找
@RequestMapping("show")
@ResponseBody // 返回一个json,通过流输出,所以加此注解
public List<Menu> show(){
return menuServiceImpl.show();
}
}
- 写页面
网友评论