ssm用后感悟

xiaoxiao2021-02-28  42

SSM整合

1. jar包

aopalliance-1.0.jarasm-3.3.1.jaraspectjweaver-1.6.11.jarcglib-2.2.2.jarcommons-beanutils-1.9.3.jarcommons-collections-3.2.jarcommons-dbcp-1.2.2.jarcommons-fileupload-1.2.2.jarcommons-io-2.4.jarcommons-lang-2.3.jarcommons-logging-1.1.1.jarcommons-logging-1.2.jarcommons-pool-1.3.jarezmorph-1.0.6.jarjackson-annotations-2.4.0.jarjackson-core-2.4.2.jarjackson-databind-2.4.2.jarjavassist-3.17.1-GA.jarjson-lib-2.4-jdk15.jarjstl-1.2.jarjunit-4.9.jarlog4j-1.2.17.jarlog4j-api-2.0-rc1.jarlog4j-core-2.0-rc1.jarmybatis-3.2.7.jarmybatis-spring-1.2.2.jarmysql-connector-java-5.1.7-bin.jarslf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jarspring-aop-4.1.3.RELEASE.jarspring-aspects-4.1.3.RELEASE.jarspring-beans-4.1.3.RELEASE.jarspring-context-4.1.3.RELEASE.jarspring-context-support-4.1.3.RELEASE.jarspring-core-4.1.3.RELEASE.jarspring-expression-4.1.3.RELEASE.jarspring-jdbc-4.1.3.RELEASE.jarspring-jms-4.1.3.RELEASE.jarspring-messaging-4.1.3.RELEASE.jarspring-tx-4.1.3.RELEASE.jarspring-web-4.1.3.RELEASE.jar

spring-webmvc-4.1.3.RELEASE.jar

2.配置文件及文件结构

3配置文件

applicationContext

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 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="10" /> <property name="maxIdle" value="5" /> </bean> <!-- Mybatis的工厂 --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 核心配置文件的位置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!-- Mapper动态代理开发 扫描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 基本包 --> <property name="basePackage" value="com.zxc.mapper"/> </bean> <!-- 注解事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启注解 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 扫描service --> <context:component-scan base-package="com.zxc.service"></context:component-scan> </beans>

db.properties

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/test_4?characterEncoding\=utf-8 jdbc.username=root jdbc.password=root

log4j.properties

# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 开启注解 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 使用Annotation自动注册Bean,只扫描@Controller --> <context:component-scan base-package="com.zxc.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!--文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600" /> <property name="maxInMemorySize" value="4096" /> <property name="defaultEncoding" value="UTF-8"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans> sqlMapConfig.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> <!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 --> <package name="com.zxc.pojo" /> </typeAliases> <mappers> <package name="com.zxc.mapper"/> </mappers> </configuration>

4.遇到的问题

controller层注解,路径,注入问题

@Controller @RequestMapping("/TEmp") public class TEmpController { @Autowired private TEmpService tEmpService; @Autowired private TPartService tPartService; /** * 检查用户名和密码是否匹配,登录 * @param request * @param model * @return */ @RequestMapping("/login.action") public String login(HttpServletRequest request){ TEmp tEmp=tEmpService.getUserByName(request.getParameter("username")); // System.out.println("pwd="+tEmp.getePsw()); String getpwd = request.getParameter("password"); String getpwd2 = tEmp.getePsw(); if (getpwd.equals(getpwd2) && tEmp.geteAdmin()==(-1)) { return "index"; } return "sign-in"; } /**

service 层注解,注入,以及通过example类查询

@Service public class TPartServiceImpl implements TPartService { @Autowired private TPartMapper dao; public List<TPart> getUsers(){ TPartExample example=new TPartExample(); com.zxc.pojo.TPartExample.Criteria criteria=example.createCriteria(); criteria.andPIdIsNotNull(); criteria.andPIsGreaterThan(0); List<TPart> list=dao.selectByExample(example); System.out.println("######"+list.get(0).getpRemark()); return list; }

criteria的方法

一、mapper接口中的方法解析

mapper接口中的函数及方法

方法功能说明int countByExample(UserExample example) thorws SQLException按条件计数int deleteByPrimaryKey(Integer id) thorws SQLException按主键删除int deleteByExample(UserExample example) thorws SQLException按条件查询String/Integer insert(User record) thorws SQLException插入数据(返回值为ID)User selectByPrimaryKey(Integer id) thorws SQLException按主键查询ListselectByExample(UserExample example) thorws SQLException按条件查询ListselectByExampleWithBLOGs(UserExample example) thorws SQLException按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。int updateByPrimaryKey(User record) thorws SQLException按主键更新int updateByPrimaryKeySelective(User record) thorws SQLException按主键更新值不为null的字段int updateByExample(User record, UserExample example) thorws SQLException按条件更新int updateByExampleSelective(User record, UserExample example) thorws SQLException按条件更新值不为null的字段

二、example实例解析

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分 xxxExample example = new xxxExample(); Criteria criteria = new Example().createCriteria();

方法说明example.setOrderByClause(“字段名 ASC”);添加升序排列条件,DESC为降序example.setDistinct(false)去除重复,boolean型,true为选择不重复的记录。criteria.andXxxIsNull添加字段xxx为null的条件criteria.andXxxIsNotNull添加字段xxx不为null的条件criteria.andXxxEqualTo(value)添加xxx字段等于value条件criteria.andXxxNotEqualTo(value)添加xxx字段不等于value条件criteria.andXxxGreaterThan(value)添加xxx字段大于value条件criteria.andXxxGreaterThanOrEqualTo(value)添加xxx字段大于等于value条件criteria.andXxxLessThan(value)添加xxx字段小于value条件criteria.andXxxLessThanOrEqualTo(value)添加xxx字段小于等于value条件criteria.andXxxIn(List<?>)添加xxx字段值在List<?>条件criteria.andXxxNotIn(List<?>)添加xxx字段值不在List<?>条件criteria.andXxxLike(“%”+value+”%”)添加xxx字段值为value的模糊查询条件criteria.andXxxNotLike(“%”+value+”%”)添加xxx字段值不为value的模糊查询条件criteria.andXxxBetween(value1,value2)添加xxx字段值在value1和value2之间条件criteria.andXxxNotBetween(value1,value2)添加xxx字段值不在value1和value2之间条件

resultType问题

返回list resultType=list中放的泛型 

而且实体类中的属性与数据库不同时,应该写list中泛型的映射如

<resultMap id="BaseResultMap" type="com.zxc.pojo.TPart" > <id column="p_id" property="pId" jdbcType="INTEGER" /> <result column="p_name" property="pName" jdbcType="VARCHAR" /> <result column="p_remark" property="pRemark" jdbcType="VARCHAR" /> <result column="p_is" property="pIs" jdbcType="INTEGER" /> </resultMap> <!-- 分页查询 --> <select id="findByPage" parameterType="int" resultMap="BaseResultMap"> select * from t_part where p_is > 0 limit #{from} ,10 </select>

5.调用流层

6.文件上传

public String fileUpload(CommonsMultipartFile file, String basePath,HttpServletRequest request) throws IOException { String filePath = ""; String fileName = file.getOriginalFilename(); System.out.println("fileName="+fileName); if (file != null && !"".equals(fileName)) { fileName = new Date().getTime() + fileName.substring(fileName.lastIndexOf(".")); String path = "d:/upfile";//request.getRealPath(basePath);上传路径,需要配置虚拟路径 System.out.println("path="+path); File baseFile = new File(path); //判断上传文件的目录是否存在,不存在则创建 if (!baseFile.exists()) { baseFile.mkdirs(); } File localFile = new File(path + "/" + fileName); try { file.transferTo(localFile); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } filePath = basePath + "/" + fileName; } return filePath; }

配置虚拟路径在server.xml中配置,应该在myeclipse中server.xml中配置如图

在server.xml的<host></host>标签做如下配置

<!-- 设置图片虚拟路径[访问时路径为/photo] --> <Context path="/upfile" docBase="D:/upfile" reloadable="true" />

js引入顺序问题

<script src="${pageContext.request.contextPath }/js/jquery-1.10.2.min.js"></script> <script src="${pageContext.request.contextPath }/js/Chart.js"></script> <!-- //chart --> <!--animate--> <link href="${pageContext.request.contextPath }/css/animate.css" rel="stylesheet" type="text/css" media="all"> <script src="${pageContext.request.contextPath }/js/wow.min.js"></script> <script> new WOW().init(); </script> <!--//end-animate--> <!----webfonts---> <link href='http://fonts.useso.com/css?family=Cabin:400,400italic,500,500italic,600,600italic,700,700italic' rel='stylesheet' type='text/css'> <!---//webfonts---> <!-- Meters graphs --> <script src="${pageContext.request.contextPath }/js/highcharts-zh_CN.js"></script> <script src="${pageContext.request.contextPath }/js/highcharts.js"></script> <script src="${pageContext.request.contextPath }/js/grid-light.js"></script>一对多问题
转载请注明原文地址: https://www.6miu.com/read-2613736.html

最新回复(0)