mybatis.xml
☞ Mybatis 配置文件
1. 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>
增加log4j
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
设置别名
<typeAliases>
<typeAlias type="com.pojo.People" alias="peo"/>
<package name="com.pojo"/>
</typeAliases>
<!-- default引用enviroment的id,表示当前所使用的环境 -->
<environments default="mysqlEnv">
<!-- 声明可以使用的换环境 -->
<environment id="mysqlEnv">
<!-- 1.1.1 JDBC 事务管理使用JDBC 原生事务管理方式 -->
<!-- 1.1.2 MANAGED 把事务管理转交给其他容器 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 1.2.1 POOLED 使用数据库连接池 -->
<!-- 1.2.2 UNPOOLED 不实用数据库连接池,和直接使用JDBC一样 -->
<!-- 1.2.3 JNDI :java 命名目录接口技术. -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssm" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
只有mapper.xml
<mappers>
<mapper resource="com/mapper/AccountMapper.xml" />
<mapper resource="com/mapper/LogMapper.xml" />
</mappers>
接口绑定
<mappers>
<package name="com.mapper" />
</mappers>
</configuration>
spring
+mybatis
☞ applicationContext.xml
☞ Spring整合Mybatis配置文件
1. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2. applicationContext.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">>
增加配置文件以后 db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties,classpath:field.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
配置环境
<!-- 数据源封装类 数据源:获取数据库连接 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- spring帮助创建sqlsessionfactory对象 -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接信息来源于DataSource -->
<property name="dataSource" ref="dataSource"></property>
<!-- 设置别名 -->
<property name="typeAliasesPackage" value="com.pojo"></property>
</bean>
<!-- 扫描器 相当于mybatis下的mapper标签下的package标签 扫描com.mapper后,创建接口对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 和factory产生关系 -->
<property name="sqlSessionFactory" ref="factory"></property>
<!-- 扫描哪个包 -->
<property name="basePackage" value="com.mapper"></property>
</bean>
创建对象
<!-- 1.通过构造方法创建 -->
<!-- id表示获取到对象标识 class创建那个类的对象 -->
<bean id="peo" class="com.pojo.People">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="张三"></constructor-arg>
<constructor-arg index="2" value="33"></constructor-arg>
</bean>
<bean id="peo1" class="com.pojo.People">
<constructor-arg name="id" value="2"></constructor-arg>
<constructor-arg name="name" value="李四"></constructor-arg>
<constructor-arg name="age" value="35"></constructor-arg>
</bean>
<!-- 这样来指定顺序,不然构造方法重载时,会从构造方法最后一个开始寻找,不去看参数的顺序 -->
<bean id="peo2" class="com.pojo.People">
<constructor-arg index="0" name="id" value="2"></constructor-arg>
<constructor-arg index="1" name="name" value="李四"></constructor-arg>
<constructor-arg index="2" name="age" value="36"></constructor-arg>
</bean>
<!-- 2.实例工厂 -->
<bean id="factory" class="com.pojo.PeopleExampleFactory"></bean>
<bean id="peo3" factory-bean="factory" factory-method="newInstance"></bean>
<!-- 3.静态工厂创建对象 -->
<bean id="peo4" class="com.pojo.PeopleStaticFactory" factory-method="newInstance"></bean>
依赖注入
<!-- 设值 注入(通过构造方法) 第一种方式 -->
<bean id="people" class="com.pojo.People">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="1" value="张三"></constructor-arg>
<constructor-arg index="2" value="33"></constructor-arg>
</bean>
<bean id="people1" class="com.pojo.People">
<constructor-arg name="id" value="2"></constructor-arg>
<constructor-arg name="name" value="李四"></constructor-arg>
<constructor-arg name="age" value="35"></constructor-arg>
</bean>
<bean id="people2" class="com.pojo.People">
<constructor-arg index="0" name="id" value="2"></constructor-arg>
<constructor-arg index="1" name="name" value="李四"></constructor-arg>
<constructor-arg index="2" name="age" value="36"></constructor-arg>
</bean>
<!-- 设值 注入(通过set方法) 第二种方式 -->
<bean id="peo" class="com.pojo.People">
<property name="id" value="222"></property>
<property name="name" value="张三"></property>
<property name="age" value="32"></property>
</bean>
<bean id="peo1" class="com.pojo.People">
<property name="id">
<value>123</value>
</property>
<property name="name">
<value>"李四"</value>
</property>
<property name="age">
<value>33</value>
</property>
</bean>
<bean id="peopleSetListMapArrayProperty"
class="com.pojo.PeopleSetListMapArrayProperty">
<!-- set赋值 -->
<property name="sets">
<set>
<value>qwe</value>
<value>dsd</value>
</set>
</property>
<!-- list赋值 第一种赋值方式 -->
<property name="list">
<list>
<value>qwe</value>
<value>www</value>
</list>
</property>
<!-- list赋值 第二种赋值方式 -->
<property name="list1" value="1,2,3"></property>
<!-- 数组 -->
<property name="strs">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="a" value="aa"></entry>
<entry key="b" value="bb"></entry>
<entry key="c" value="cc"></entry>
</map>
</property>
<!-- property -->
<property name="prop">
<props>
<prop key="key">value</prop>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
</props>
</property>
<!-- 对象 -->
<property name="desk" ref="dest"></property>
</bean>
<bean id="dest" class="com.pojo.Desk">
<property name="id" value="1"></property>
<property name="price" value="12"></property>
</bean>
自动注入
<bean id="teacher" class="com.test.Teacher"></bean>
<bean id="people" class="com.test.People" autowire="byName">
<property name="teacher" ref="teacher"></property> <!-- 传统的赋值语句 -->
<!-- autowire="default" 引用最上面设置的值 -->
<!-- autowire="byName" 找其他bean的ID -->
<!-- autowire="byType" 找其他bean的type,如果有多个一样的类型会报错 -->
<!-- autowire="constructor" 和byName一样,不过构造方法的参数名称要和bean的Id相等 -->
<bean id="people1" class="com.test.People" autowire="byType"></bean>
<bean id="people2" class="com.test.People" autowire="constructor"></bean>
<bean id="people2" class="com.test.People" autowire="default"></bean>
Scope
<bean id="teacher3" class="com.pojo.Teacher" scope="prototype"></bean>
<bean id="teacher4" class="com.pojo.Teacher" scope="singleton"></bean>
AOP schema-based方式
<!-- 配置通知类的对象,在切面中引入 -->
<bean id="mybeforeadvice" class="com.advice.MyBeforeAdvice"></bean>
<bean id="myafteradvice" class="com.advice.MyAfterAdvice"></bean>
<bean id="myArroundAdvice" class="com.advice.MyArroundAdvice"></bean>
<bean id="myThrowAdvice" class="com.advice.MyThrowAdvice"></bean>
<!--配置Demo类 测试时候使用 -->
<bean id="demo" class="com.test.Demo"></bean>
<!-- 错误提示:Bean named 'userService' must be of type [com.service.impl.UsersServiceImpl], but was actually of type [com.sun.proxy.$Proxy33]-->
<!-- 默认情况下spring使用的是JDK动态代理,JDK不能把代理对象转换成实现类,所以 usersService = ac.getBean("userService", UsersServiceImpl.class); 会报错,用cglib动态代理-->
代码里有ac.getbean() 然后强转成实现类的时候需要配置如下代码
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<!-- 配置切面 -->
<!-- schema-based方式配置切点 -->
<aop:config>
<!-- * 通配符 匹配任意方法名 任意类名 任意一级包名 -->
<aop:pointcut expression="execution(* com.test.Demo.demo1())" id="mypoint1" />
<aop:pointcut expression="execution(* com.test.Demo.demo2())" id="mypoint2" />
<aop:pointcut expression="execution(* com.test.Demo.demo3())" id="mypoint3" />
<aop:pointcut expression="execution(* com.test.Demo.demo4())" id="mypoint4" />
<!-- 希望匹配任意方法参数方法(..) -->
<aop:pointcut expression="execution(* com.test.Demo.demo5(..))" id="mypoint5" />
<aop:pointcut expression="execution(* com.test.Demo.*(..))" id="mypoint" />
<!-- 通知 -->
<aop:advisor advice-ref="mybeforeadvice" pointcut-ref="mypoint1" />
<aop:advisor advice-ref="myafteradvice" pointcut-ref="mypoint2" />
<aop:advisor advice-ref="myArroundAdvice" pointcut-ref="mypoint3" />
<aop:advisor advice-ref="myThrowAdvice" pointcut-ref="mypoint4" />
<aop:advisor advice-ref="mybeforeadvice" pointcut-ref="mypoint5" />
<aop:advisor advice-ref="myafteradvice" pointcut-ref="mypoint5" />
</aop:config>
AOP aspectJ方式
<bean id="demo" class="com.test.Demo"></bean>
<bean id="myadvice" class="com.advice.MyAdvice"></bean>
<aop:config>
<aop:aspect ref="myadvice">
<aop:pointcut expression="execution(* com.test.Demo.demo1(String,int)) and args(name111,age111)" id="mypoint1"/>
<aop:pointcut expression="execution(* com.test.Demo.demo2(String)) and args(name11)" id="mypoint2"/>
<aop:pointcut expression="execution(* com.test.Demo.demo3(String)) and args(name12)" id="mypoint3"/>
<aop:pointcut expression="execution(* com.test.Demo.demo4(String)) and args(name13)" id="mypoint4"/>
<aop:pointcut expression="execution(* com.test.Demo.demo5())" id="mypoint5"/>
<aop:pointcut expression="execution(* com.test.Demo.demo6())" id="mypoint6"/>
<aop:before method="mybefore" pointcut-ref="mypoint1" arg-names="name111,age111"/>
<aop:before method="mybefore1" pointcut-ref="mypoint2" arg-names="name11"/>
<!-- 不管是否出异常都会走 -->
<aop:after method="myafter1" pointcut-ref="mypoint3" arg-names="name12"/>
<!-- 如果出异常就不走了 -->
<aop:after-returning method="myafter2" pointcut-ref="mypoint4" arg-names="name13"/>
<aop:around method="myarround" pointcut-ref="mypoint5"/>
<aop:after-throwing method="myexception" pointcut-ref="mypoint6" throwing="e1"/>
</aop:aspect>
</aop:config>
AOP 注解方式
<!-- spring 不会自动去寻找注解,必须告诉spring 哪些包下的类中可能有注解 -->
<context:component-scan base-package="com.advice,com.test"></context:component-scan>
<!-- true 使用cglib动态代理 -->
<!-- false 使用jdk动态代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
事务
<!-- spring-jdbc中 -->
<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txadvice" transaction-manager="txmanager">
<tx:attributes>
<!-- 哪个方法需要添加事务 -->
<!-- 所有以ins开头都添加事务 -->
<tx:method name="ins*" propagation="REQUIRED" />
<tx:method name="upd*" isolation="SERIALIZABLE" />
<tx:method name="del*" rollback-for="java.lang.Exception" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
<aop:config>
<!-- 切点范围设计大点 -->
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="mypoint" />
<aop:advisor advice-ref="txadvice" pointcut-ref="mypoint" />
</aop:config>
</beans>
3. SSM ☞ springmvc
+ spring
+ mybatis
1.web.xml
4组标签
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<!-- Spring -->
<!-- 上下文参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码过滤器 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2. applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" default-autowire="byName">
<!-- 注解扫描 -->
<!-- Spring只扫描Service SpringMVC只扫描Controller 如果Spring扫描Controller将会出现声明式事务无效的效果 -->
<context:component-scan base-package="com.service.impl"></context:component-scan>
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- SqlSessionFactory -->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 别名 -->
<property name="typeAliasesPackage" value="com.pojo"></property>
</bean>
<!-- 扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="factory"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 声明式事务 -->
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<tx:method name="ins*"/>
<tx:method name="del*"/>
<tx:method name="upd*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- impl下的任意类的任意方法的任意参数 -->
<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
3. springmvc.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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解,只扫描controller包 -->
<context:component-scan base-package="com.controller"></context:component-scan>
<!-- 注解驱动,注册HandlerMapping和HandlerAdapter -->
<!-- 包含了 -->
<!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
<!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 设置静态资源 -->
<!-- web.xml里配置了<url-pattern>/</url-pattern>,因为除了jsp不拦截别的都拦截,所以指定下面路径 -->
<!-- 前面为实际存储地址,后面为url访问时的地址 -->
<!-- /表示项目根目录 js/*表示该文件夹下的所有子文件 js/**表示该文件夹下的所有子文件及子文件夹中的文件 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4. SSM项目轮廓
Entity 层
(实体类)
public class Menu {
private int id;
private String name;
private int pid;
private List<Menu> children;
}
Dao
层(数据库操作)
public interface MenuMapper {
List<Menu> selByPid(int pid);
}
<mapper namespace="com.mapper.MenuMapper">
<resultMap type="menu" id="mymap">
<id property="id" column="id" />
<collection property="children" select="com.mapper.MenuMapper.selByPid" column="id"></collection>
</resultMap>
<select id="selByPid" resultMap="mymap" parameterType="int">
select * from menu where pid = #{0}
</select>
</mapper>
Service 层
(处理业务)
public interface MenuService {
List<Menu> show();
}
@Service
public class MenuServiceImpl implements MenuService{
@Resource
private MenuMapper meneMapper;
@Override
public List<Menu> show() {
List<Menu> selByPid = meneMapper.selByPid(0);
return selByPid;
}
}
Controller 层
(控制层)
@Controller
public class MenuController {
@Resource
private MenuService menuServiceImpl;
@RequestMapping("show")
@ResponseBody
public List<Menu> show() {
List<Menu> show = menuServiceImpl.show();
return show;
}
}
其他配置文件
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
log4j.properties
log4j.rootCategory=ERROR, CONSOLE ,LOGFILE
log4j.logger.com.mapper=DEBUG
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%C %d{YYYY-MM-dd hh:mm:ss} %m %n
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:/mavensshmy.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%m %n