SSM基础整合&&Mybatis高级查询(动态sql)

本文详细介绍了如何进行SSM(Spring、SpringMVC、Mybatis)的基础整合,包括新建动态web项目、导入相关jar包、配置文件创建等步骤。接着,通过测试查询并返回页面,展示了数据库操作和前后端交互。最后,深入探讨了Mybatis的高级查询功能,如动态sql标签(if、where、trim、choose等),并提供了实际案例说明如何实现条件拼接和动态条件判断,以实现灵活的数据库查询操作。
摘要由CSDN通过智能技术生成

一. SSM整合

1.1 新建一个动态web项目

在这里插入图片描述

1.2 导入需要的jar包

  • jstl:
    • jstl.jar
    • standard.jar
  • mybatis:
    • asm-3.3.1.jar
    • cglib-2.2.2.jar
    • commons-logging-1.1.1.jar
    • javassist-3.17.1-GA.jar
    • log4j-1.2.17.jar
    • mybatis-3.2.1.jar
    • slf4j-api-1.7.2.jar
    • slf4j-log4j12-1.7.2.jar
  • sevlet+jsp:
    • jsp-api.jar
    • servlet-api.jar
  • spring:
    • commons.logging-1.1.1.jar
    • spring-aop-4.1.2.RELEASE.jar
    • spring-beans-4.1.2.RELEASE.jar
    • spring-context-4.1.2.RELEASE.jar
    • spring-core-4.1.2.RELEASE.jar
    • spring-expression-4.1.2.RELEASE.jar
    • spring-test-4.1.2.RELEASE.jar
  • spring+mybatis
    • com.springsource.org.aopalliance-1.0.0.jar
    • com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    • commons.dbcp-1.2.2.jar
    • commons.pool-1.5.3.jar
    • mybatis-spring-1.2.0.jar
    • spring-jdbc-4.1.2.RELEASE.jar
    • spring-tx-4.1.2.RELEASE.jar
  • springmvc
    • commons-fileupload-1.3.1.jar
    • commons-io-2.2.jar
    • jackson-annotations-2.5.0.jar
    • jackson-core-2.5.0.jar
    • jackson-databind-2.5.0.jar
    • spring-web-4.1.2.RELEASE.jarss
    • spring-webmvc-4.1.2.RELEASE.jar
  • 数据库连接:
    • mysql-connector-java-5.1.26-bin.jar
  • 使用的服务器为tomcat,需要的童鞋可以百度自行下载配置

在这里插入图片描述

1.3 创建配置文件

  1. 新建资源文件夹source folder:resources

在这里插入图片描述

  1. 新建数据库配置文件:db.properties
#驱动全限定类名
jdbc.driverClassName=com.mysql.jdbc.Driver
#数据库连接地址
jdbc.url=jdbc:mysql://127.0.0.1:3306/你的数据库名?useUnicode=true&characterEncoding=UTF-8
#数据库用户名
jdbc.username=你的数据库用户名
#数据库密码
jdbc.password=你的数据库密码
  1. 新建log4j配置文件:log4j.properties
控制台输出+自定义布局
log4j.rootLogger=DEBUG,my
#指定输出器
log4j.appender.my=org.apache.log4j.ConsoleAppender
#指定布局器(自定义布局)
#指定布局为自定义布局
log4j.appender.my.layout=org.apache.log4j.PatternLayout
#指定在自定义布局的格式,%d -- 表示当前系统时间,%t -- 执行该业务的线程名称,%p -- 日记器的级别,-5 -- 5表示输出字符的个数,符号表示右对齐
#%c -- 表示指定业务所在的类的完全限定名(包名.类名),%m -- 输出额外信息,%n -- 表示换行
log4j.appender.my.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置package(可以是自定义的包也可以是api的包)输出级别
log4j.logger.org.springframework=info
log4j.logger.cn.xxxx=debug

  1. Spirng配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	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.xsd
	http://www.springframework.org/schema/mvc
	http://www.springframework.org/schema/mvc/spring-mvc.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	">
	<!-- 1.查找数据库配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 2.Spring管理连接池 -->
	<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- 3.Spring管理SqlSessionFactory -->
	<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="basicDataSource"></property>
		<!-- 配置mybatis(mapper)映射路径 -->
		<property name="mapperLocations" value="classpath:cn/xxxx/mapper/*Mapper.xml"></property>
		<!-- 配置别名 -->
		<property name="typeAliasesPackage" value="cn.xxxx.domain"></property>
	</bean>
	<!-- 4.扫描配置Mapper:动态创建mapper包中所有接口的动态示例 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.xxxx.mapper"></property>
	</bean>
	
	<!-- 5.扫描Service交给spring管理 -->
	<context:component-scan base-package="cn.xxxx.service"></context:component-scan>
</beans>
  1. SpringMVC配置文件: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" 
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- SpringMVC测试 -->
<!-- <context:component-scan base-package="cn.xxxx._02Springmvc"></context:component-scan>  --> 

    <!-- 1. 配置扫描包路径子容器只扫描controller包 -->
	<context:component-scan base-package="cn.xxxx.controller"></context:component-scan>
	
	<!-- 2. 配置静态资源放行 -->
	<mvc:default-servlet-handler/>
	
	<!-- 3. 配置开启Spring对mvc的支持,支持@RequestMapping注解 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 4. 配置视图解析器 -->
	<bean class="org.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值