SSM整合
1. jar包
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver-1.6.11.jar
cglib-2.2.2.jar
commons-beanutils-1.9.3.jar
commons-collections-3.2.jar
commons-dbcp-1.2.2.jar
commons-fileupload-1.2.2.jar
commons-io-2.4.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
commons-logging-1.2.jar
commons-pool-1.3.jar
ezmorph-1.0.6.jar
jackson-annotations-2.4.0.jar
jackson-core-2.4.2.jar
jackson-databind-2.4.2.jar
javassist-3.17.1-GA.jar
json-lib-2.4-jdk15.jar
jstl-1.2.jar
junit-4.9.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.7-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.3.RELEASE.jar
spring-aspects-4.1.3.RELEASE.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE.jar
spring-context-support-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE.jar
spring-jdbc-4.1.3.RELEASE.jar
spring-jms-4.1.3.RELEASE.jar
spring-messaging-4.1.3.RELEASE.jar
spring-tx-4.1.3.RELEASE.jar
spring-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>
一对多问题