对SSM进行一个简单的配置

4 篇文章 0 订阅
2 篇文章 0 订阅

SSM框架配置

一.先对spring的xml文件进行配置

  • 创建一个db.properties文件用于存放数据库登录信息
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_steam?useSSL=false
jdbc.user=root
jdbc.password=612861288
1.对service进行注解配置
  • applicationContext_service.xml
<?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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
		<!-- 开启注解扫描 -->
		<context:component-scan base-package="com.steam.service"/>
</beans>
2.对mapper进行配置:对mybatis和mapper工厂进行装配
  • applicationContext_mapper.xml

注意:在配置mapper工厂的时候mapper.xml和mapper接口得放在同一个文件夹并且名字得相同

<?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 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
		
		<!-- 读取配置文件 -->
		<context:property-placeholder location="classpath:config/db.properties"/>		
		
		<!-- 配置 dataSource -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 配置mybatis -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="typeAliasesPackage" value="com.steam.bean"></property>
	</bean>
		
		<!-- mapper工厂 -->
	<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.steam.mapper"/>
	</bean>
</beans>
3.对事务进行配置
  • applicationContext_transaction.xml
<?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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
		<!-- 需要事务核心管理器 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- 开启事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

二. 对springMVC进行配置

  • springmvc.xml
<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		<context:property-placeholder location="classpath:config/dictTypeId.properties"/>
		<!-- 开启注解扫描 -->
		<context:component-scan base-package="com.steam.controller"/>
		<!-- 开启注解驱动 -->
		<mvc:annotation-driven />
		<!-- 配置视图解析器 -->
		<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
		</bean>
</beans>

三. 对web.xml进行装配

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>ssm_steam</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 静态资源放行 -->
  
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.ttf</url-pattern>
<url-pattern>*.woff</url-pattern>
<url-pattern>*.js</url-pattern>
<url-pattern>*.png</url-pattern>
<url-pattern>*.jpg</url-pattern>
<url-pattern>*.gif</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>
  //对spring进行装配
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:config/applicationContext_*.xml</param-value>
  </context-param>

  //对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:config/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>
  
</web-app>
  • 在每次启动服务器是有时候会遇到静态拦截问题,并且是上一次使用没问题但重启就有时候会报错,这个时候可以尝试在web.xml文件中加入或删除一个空格就可以正常启动了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSMSpring+SpringMVC+MyBatis的缩写,它是一种Java企业级应用开发框架。下面是一个简单SSM后台的实现步骤: 1. 配置数据库 在`src/main/resources`目录下创建`jdbc.properties`文件,配置数据库连接信息,例如: ``` jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456 ``` 2. 配置Spring框架 在`src/main/resources`目录下创建`applicationContext.xml`文件,配置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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自动扫描包 --> <context:component-scan base-package="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置MyBatis --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:mapper/**/*.xml"/> </bean> <!-- 配置dataSource --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="20"/> <property name="initialSize" value="1"/> <property name="maxWait" value="60000"/> <property name="minIdle" value="1"/> <property name="validationQuery" value="SELECT 1 FROM DUAL"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="testWhileIdle" value="true"/> </bean> <!-- 开启Spring事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> </beans> ``` 3. 配置MyBatis框架 在`src/main/resources`目录下创建`mybatis-config.xml`文件,配置MyBatis框架的相关信息,例如: ``` <?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.example.entity"/> </typeAliases> <!-- 配置插件 --> <plugins> <plugin interceptor="com.example.interceptor.PageInterceptor"/> </plugins> <!-- 配置分页插件 --> <objectFactory type="com.example.factory.PageFactory"/> <!-- 配置mapper --> <mappers> <mapper resource="mapper/ExampleMapper.xml"/> </mappers> </configuration> ``` 4. 编写Controller 在`src/main/java`目录下创建一个Controller类,例如: ``` @Controller @RequestMapping("/example") public class ExampleController { @Autowired private ExampleService exampleService; @RequestMapping("/list") public ModelAndView list() { List<Example> exampleList = exampleService.getExampleList(); ModelAndView mv = new ModelAndView("example/list"); mv.addObject("exampleList", exampleList); return mv; } @RequestMapping("/add") public ModelAndView add(Example example) { exampleService.addExample(example); ModelAndView mv = new ModelAndView("redirect:/example/list"); return mv; } @RequestMapping("/delete") public String delete(int id) { exampleService.deleteExample(id); return "redirect:/example/list"; } } ``` 5. 编写Service 在`src/main/java`目录下创建一个Service类,例如: ``` @Service public class ExampleService { @Autowired private ExampleMapper exampleMapper; public List<Example> getExampleList() { return exampleMapper.getExampleList(); } public void addExample(Example example) { exampleMapper.addExample(example); } public void deleteExample(int id) { exampleMapper.deleteExample(id); } } ``` 6. 编写Mapper 在`src/main/resources/mapper`目录下创建一个Mapper接口和对应的XML文件,例如: ``` public interface ExampleMapper { List<Example> getExampleList(); void addExample(Example example); void deleteExample(int id); } <mapper namespace="com.example.mapper.ExampleMapper"> <select id="getExampleList" resultType="com.example.entity.Example"> SELECT * FROM example </select> <insert id="addExample" parameterType="com.example.entity.Example"> INSERT INTO example (name, age) VALUES (#{name}, #{age}) </insert> <delete id="deleteExample"> DELETE FROM example WHERE id = #{id} </delete> </mapper> ``` 7. 编写JSP页面 在`src/main/webapp/WEB-INF/views/example`目录下创建JSP页面,例如: ``` <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Example List</title> </head> <body> <table> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Operation</th> </tr> <c:forEach items="${exampleList}" var="example"> <tr> <td>${example.id}</td> <td>${example.name}</td> <td>${example.age}</td> <td> <a href="/example/delete?id=${example.id}">Delete</a> </td> </tr> </c:forEach> </table> <hr/> <form action="/example/add" method="post"> <label>Name:</label> <input type="text" name="name"/><br/> <label>Age:</label> <input type="text" name="age"/><br/> <input type="submit" value="Add"/> </form> </body> </html> ``` 以上就是一个简单SSM后台的实现步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值