SSM配置文件【代码库】


tags: 代码库


#log4j.properties#

# Rules reminder:
# DEBUG < INFO < WARN < ERROR < FATAL

# Global logging configuration
log4j.rootLogger=debug,stdout

# My logging configuration...
log4j.logger.cn.jbit.mybatisdemo=DEBUG


## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

log4j.logger.org.apache.ibatis=DEBUG
## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG
log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG
## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
复制代码

#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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


    <!-- 配置数据源,记得去掉myBatis-config.xml的数据源相关配置 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl"
                  value="jdbc:mysql://localhost:3306/scm?useUnicode=true&amp;characterEncoding=UTF-8" />
        <property name="user" value="root" />
        <property name="password" value="root" />
    </bean>
    <!-- 配置session工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:myBatis-config.xml" />
        <!--配置扫描式加载SQL映射文件,记得去掉mybatis-config配置-->
      <property name="mapperLocations" value="classpath:zhongfucheng/dao/*.xml"/>


    </bean>

    <!-- 配置事务管理器,管理数据源事务处理 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置事务通知 -->
    <tx:advice id="advice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 默认只处理运行时异常,可加rollback-for="Exception/Throwable"等处理所有异常或包括错误 -->
            <tx:method name="insert*" propagation="REQUIRED"
                       rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                       rollback-for="Exception" />
            <tx:method name="delete*" propagation="REQUIRED"
                       rollback-for="Exception" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面织入的范围,后边要把事务边界定在service层 -->
    <aop:config>
        <aop:advisor advice-ref="advice"
                     pointcut="execution(* cn.itcast.scm.service.impl.*.*(..))" />
    </aop:config>
    <!-- 配置SessionTemplate,已封装了繁琐的数据操作,提交事务都不用做了。 -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <!-- 自动扫描组件,要把controller去除,他们是在spring-mvc.xml中配置,如果不去除会影响事务管理。 -->
    <context:component-scan base-package="cn.itcast">
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 配置 转换器,对于在basePackage设置的包(包括子包)下的接口类,
    如果接口类的全类名在Mapper.xml文件中和定义过命名空间一致,
     将被转换成spring的BEAN,在调用 
        的地方通过@Autowired方式将可以注入接口实例 -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />

        <!--这里包名留意要不要改-->
        <property name="basePackage" value="zhongfucheng.dao" />
        </bean>


</beans>
复制代码

#Mybatis映射文件#


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="zhongfucheng.entity.DeptMapper">
    <resultMap type="Dept" id="deptResultMap">
        <id property="deptId" column="dept_id" />
        <result property="deptName" column="dept_name" />
        <result property="deptAddress" column="dept_address" />
    </resultMap>
    <!-- id和命名空间用来定位SQL语句,parameterType表示参数的类型,resultMap返回类型 -->
    <select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
        <!--参数的写法#{deptID} -->
        select * from dept where dept_id=#{deptID}
    </select>

    <insert id="insertDept" parameterType="Dept">
        insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress});
    </insert>

</mapper>
复制代码

#spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    ">
    <!-- 同时开启json格式的支持 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- <context:component-scan base-package="*"/> -->
    
    <!-- 扫描所有的controller 但是不扫描service -->
    <context:component-scan base-package="zhongfucheng">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    
    
    
</beans>
复制代码

#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_3_1.xsd"
         version="3.1">


    <!--Spring监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>


    <!--Mvc分配器-->
    <servlet>
        <servlet-name>mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!--中文过滤器-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
复制代码

如果您觉得这篇文章帮助到了您,可以给作者一点鼓励

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的存信息管理系统的SSM前后端代码实现示例: 后端代码(使用Spring+SpringMVC+MyBatis): 1. 配置数据连接信息 在applicationContext.xml中配置数据连接信息: ``` <!-- 数据连接信息 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/inventory"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> ``` 2. 配置Mapper接口和Mapper映射文件 在mybatis-config.xml中配置Mapper接口和Mapper映射文件: ``` <!-- 配置Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> <!-- 配置Mapper映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:com/example/mapper/*.xml"/> </bean> ``` 3. 定义Mapper接口和Mapper映射文件 定义InventoryMapper接口和InventoryMapper.xml文件: InventoryMapper.java ``` public interface InventoryMapper { // 查询所有存信息 List<Inventory> getAllInventories(); // 根据ID查询存信息 Inventory getInventoryById(Integer id); // 添加存信息 int addInventory(Inventory inventory); // 修改存信息 int updateInventory(Inventory inventory); // 删除存信息 int deleteInventory(Integer id); } ``` InventoryMapper.xml ``` <mapper namespace="com.example.mapper.InventoryMapper"> <!-- 查询所有存信息 --> <select id="getAllInventories" resultType="com.example.entity.Inventory"> select * from inventory </select> <!-- 根据ID查询存信息 --> <select id="getInventoryById" parameterType="int" resultType="com.example.entity.Inventory"> select * from inventory where id=#{id} </select> <!-- 添加存信息 --> <insert id="addInventory" parameterType="com.example.entity.Inventory"> insert into inventory(name, unit, quantity) values(#{name}, #{unit}, #{quantity}) </insert> <!-- 修改存信息 --> <update id="updateInventory" parameterType="com.example.entity.Inventory"> update inventory set name=#{name}, unit=#{unit}, quantity=#{quantity} where id=#{id} </update> <!-- 删除存信息 --> <delete id="deleteInventory" parameterType="int"> delete from inventory where id=#{id} </delete> </mapper> ``` 4. 定义实体类Inventory ``` public class Inventory { private Integer id; private String name; private String unit; private Integer quantity; // 省略getter和setter方法 } ``` 5. 定义Service接口和Service实现类 定义InventoryService接口和InventoryServiceImpl实现类: InventoryService.java ``` public interface InventoryService { // 查询所有存信息 List<Inventory> getAllInventories(); // 根据ID查询存信息 Inventory getInventoryById(Integer id); // 添加存信息 int addInventory(Inventory inventory); // 修改存信息 int updateInventory(Inventory inventory); // 删除存信息 int deleteInventory(Integer id); } ``` InventoryServiceImpl.java ``` @Service public class InventoryServiceImpl implements InventoryService { @Autowired private InventoryMapper inventoryMapper; // 查询所有存信息 public List<Inventory> getAllInventories() { return inventoryMapper.getAllInventories(); } // 根据ID查询存信息 public Inventory getInventoryById(Integer id) { return inventoryMapper.getInventoryById(id); } // 添加存信息 public int addInventory(Inventory inventory) { return inventoryMapper.addInventory(inventory); } // 修改存信息 public int updateInventory(Inventory inventory) { return inventoryMapper.updateInventory(inventory); } // 删除存信息 public int deleteInventory(Integer id) { return inventoryMapper.deleteInventory(id); } } ``` 6. 定义Controller类 定义InventoryController类: ``` @Controller @RequestMapping("/inventory") public class InventoryController { @Autowired private InventoryService inventoryService; // 查询所有存信息 @RequestMapping(value = "/getAllInventories", method = RequestMethod.GET) public ModelAndView getAllInventories() { List<Inventory> inventories = inventoryService.getAllInventories(); ModelAndView modelAndView = new ModelAndView("inventoryList"); modelAndView.addObject("inventories", inventories); return modelAndView; } // 跳转到添加存信息页面 @RequestMapping(value = "/toAddInventory", method = RequestMethod.GET) public ModelAndView toAddInventory() { ModelAndView modelAndView = new ModelAndView("addInventory"); return modelAndView; } // 添加存信息 @RequestMapping(value = "/addInventory", method = RequestMethod.POST) public ModelAndView addInventory(@ModelAttribute Inventory inventory) { int result = inventoryService.addInventory(inventory); ModelAndView modelAndView = new ModelAndView("redirect:/inventory/getAllInventories"); return modelAndView; } // 跳转到修改存信息页面 @RequestMapping(value = "/toUpdateInventory", method = RequestMethod.GET) public ModelAndView toUpdateInventory(@RequestParam("id") Integer id) { Inventory inventory = inventoryService.getInventoryById(id); ModelAndView modelAndView = new ModelAndView("updateInventory"); modelAndView.addObject("inventory", inventory); return modelAndView; } // 修改存信息 @RequestMapping(value = "/updateInventory", method = RequestMethod.POST) public ModelAndView updateInventory(@ModelAttribute Inventory inventory) { int result = inventoryService.updateInventory(inventory); ModelAndView modelAndView = new ModelAndView("redirect:/inventory/getAllInventories"); return modelAndView; } // 删除存信息 @RequestMapping(value = "/deleteInventory", method = RequestMethod.GET) public ModelAndView deleteInventory(@RequestParam("id") Integer id) { int result = inventoryService.deleteInventory(id); ModelAndView modelAndView = new ModelAndView("redirect:/inventory/getAllInventories"); return modelAndView; } } ``` 前端代码(使用JSP+Bootstrap): 1. inventoryList.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>存信息列表</title> <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet"> <link href="${pageContext.request.contextPath}/static/css/bootstrap-theme.min.css" rel="stylesheet"> <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script> <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h1>存信息列表</h1> <table class="table table-bordered table-striped table-hover"> <thead> <tr> <th>ID</th> <th>名称</th> <th>单位</th> <th>数量</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${inventories}" var="inventory"> <tr> <td>${inventory.id}</td> <td>${inventory.name}</td> <td>${inventory.unit}</td> <td>${inventory.quantity}</td> <td> <a href="${pageContext.request.contextPath}/inventory/toUpdateInventory?id=${inventory.id}" class="btn btn-primary">修改</a> <a href="${pageContext.request.contextPath}/inventory/deleteInventory?id=${inventory.id}" class="btn btn-danger">删除</a> </td> </tr> </c:forEach> </tbody> </table> <a href="${pageContext.request.contextPath}/inventory/toAddInventory" class="btn btn-success">添加存信息</a> </div> </body> </html> ``` 2. addInventory.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加存信息</title> <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet"> <link href="${pageContext.request.contextPath}/static/css/bootstrap-theme.min.css" rel="stylesheet"> <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script> <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h1>添加存信息</h1> <form class="form-horizontal" action="${pageContext.request.contextPath}/inventory/addInventory" method="post"> <div class="form-group"> <label class="control-label col-sm-2">名称:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="name" placeholder="请输入名称"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">单位:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="unit" placeholder="请输入单位"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">数量:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="quantity" placeholder="请输入数量"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">添加</button> </div> </div> </form> </div> </body> </html> ``` 3. updateInventory.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改存信息</title> <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet"> <link href="${pageContext.request.contextPath}/static/css/bootstrap-theme.min.css" rel="stylesheet"> <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script> <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h1>修改存信息</h1> <form class="form-horizontal" action="${pageContext.request.contextPath}/inventory/updateInventory" method="post"> <input type="hidden" name="id" value="${inventory.id}"> <div class="form-group"> <label class="control-label col-sm-2">名称:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="name" value="${inventory.name}"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">单位:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="unit" value="${inventory.unit}"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2">数量:</label> <div class="col-sm-10"> <input type="text" class="form-control" name="quantity" value="${inventory.quantity}"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">修改</button> </div> </div> </form> </div> </body> </html> ``` 以上是一个简单的存信息管理系统的SSM前后端代码实现示例,仅供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值