mybatis是一个java持久层框架,java中操作关系型数据库用的是jdbc,其中hibernate是jdbc的一个封装,而mybatis也是对jdbc的一个封装(准确说是一个半封装)。
1、mybatis使用mapper代理的开发方式
2、输入映射类型和输出映射类型。
其中 id 为 mapper (dao)接口的方法名,parameterType 为方法中的参数类型,resultMap 为方法返回值类型
3、动态sql (如下)
4、相关的配置信息(ssm框架)
springmvc :
spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:util="http://www.springframework.org/schema/util" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/beans 11 http://www.springframework.org/schema/beans/spring-beans.xsd 12 http://www.springframework.org/schema/util 13 http://www.springframework.org/schema/util/spring-util.xsd 14 http://www.springframework.org/schema/mvc 15 http://www.springframework.org/schema/mvc/spring-mvc.xsd 16 "> 17 <!-- 支持注解 --> 18 <mvc:annotation-driven></mvc:annotation-driven> 19 <!-- 定义注解要扫描的包 --> 20 <context:component-scan base-package="com.ssm.handler"></context:component-scan> 21 <!-- 对静态资源进行处理 --> 22 <mvc:default-servlet-handler/> 23 <!-- 配置视图解析器 --> 24 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 25 <property name="prefix" value="/"/> 26 <property name="suffix" value=".jsp"/> 27 </bean> 28 <bean id="multipartResolver" 29 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 30 <!-- 设置上传文件的最大尺寸为1MB --> 31 <property name="maxUploadSize"> 32 <value>1048576</value> 33 </property> 34 </bean> 35 36 37 38 </beans>
spring:
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/beans 12 http://www.springframework.org/schema/beans/spring-beans.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop.xsd 15 http://www.springframework.org/schema/util 16 http://www.springframework.org/schema/util/spring-util.xsd 17 http://www.springframework.org/schema/tx 18 http://www.springframework.org/schema/tx/spring-tx.xsd 19 "> 20 21 <!-- 1.读取jdbc.properties配置文件 --> 22 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 23 <property name="locations" value="classpath:db.properties"/> 24 </bean> 25 26 <!-- 2.配置jdbc的dataSource --> 27 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 28 <property name="driverClassName" value="${jdbc.driverClassName}"/> 29 <property name="url" value="${jdbc.url}"/> 30 <property name="username" value="${jdbc.username}"/> 31 <property name="password" value="${jdbc.password}"/> 32 </bean> 33 34 <!-- 3.配置mybatis的sqlSessionFactory --> 35 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 36 <property name="dataSource" ref="dataSource"/> 37 <!-- 引入mybatis配置文件 --> 38 <property name="configLocation" value="classpath:myBatis/myBatis.xml"></property> 39 </bean> 40 41 <!-- 4.配置mybatis自动扫描sql映射文件/接口 --> 42 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 43 <property name="basePackage" value="com.ssm.mapper"></property> 44 </bean> 45 46 47 <!-- 5.定义事务所在的bean --> 48 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 49 <property name="dataSource" ref="dataSource"></property> 50 </bean> 51 52 <!-- 6.支持注解驱动的事务管理,指定事务管理器 --> 53 <tx:annotation-driven transaction-manager="transactionManager" /> 54 55 <!-- 7. aspectj支持自动代理实现AOP功能 --> 56 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> 57 58 <!-- 8.定义事务的切面和切入点信息 --> 59 <!-- 定义事务的属性 --> 60 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 61 <tx:attributes> 62 <tx:method name="find*" read-only="true"/> 63 <tx:method name="list*" read-only="true"/> 64 <tx:method name="get*" read-only="true"/> 65 <tx:method name="select*" read-only="true"/> 66 <tx:method name="*" propagation="REQUIRED" read-only="false"/> 67 </tx:attributes> 68 </tx:advice> 69 <!-- 定义事务所使用的切入点 --> 70 <aop:config> 71 <aop:pointcut expression="execution(* com.ssm.service.impl.*.*(..))" id="pointcut"/> 72 <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> 73 </aop:config> 74 <!-- 定义spring扫描的包 --> 75 <context:component-scan base-package="com.ssm.service.impl"></context:component-scan> 76 <context:component-scan base-package="com.ssm.mapper"></context:component-scan> 77 78 </beans>
mybatis (可以写入spring配置中)
mybatis.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> <!-- properties属性配置文件的引入 --> <!-- setting设置,调整mybaits运行行为 --> <settings> <!-- 匹配下划线到驼峰式命名法 --> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="cacheEnabled" value="true"/> <!-- 延迟加载总开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 是否积极加载 --> <!-- <setting name="aggressivelazyLoading" value="false"/> --> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <!-- 类起别名 --> <typeAliases> <package name="com.ssm.entity"/> </typeAliases> </configuration>
数据库链接配置:
db.properties
1 #jdbc.driverClassName=com.mysql.jdbc.Driver 2 #jdbc.url=jdbc:mysql://localhost:3306/数据库名l 3 #jdbc.username=root 4 #jdbc.password=密码 5 6 jdbc.driverClassName=oracle.jdbc.driver.OracleDriver 7 jdbc.url=jdbc:oracle:thin:@localhost:1521:实例名 8 jdbc.username=用户 9 jdbc.password=密码
log4j 配置;
log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n 7 8 log4j.logger.java.sql.ResultSet=INFO 9 log4j.logger.org.apache=INFO 10 log4j.logger.java.sql.Connection=DEBUG 11 log4j.logger.java.sql.Statement=DEBUG 12 log4j.logger.java.sql.PreparedStatement=DEBUG