目录结构如下:
1.web.xml配置 <context-param> <param-name>contextConfigLocation</param-name> <!--spring配置文件路径及文件名,根据自己实际情况改--> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 2.spring配置文件,放到类路径下 <?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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 包扫描, 我用的注解方式, 根据自己实际目录改 --> <context:component-scan base-package="action,service,dao"></context:component-scan> <!-- 连接池 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--加载mybatis配置文件,根据自己目录改--> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> </bean> <!--以下是事务配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* service.*.*(..))" id="p"/> <aop:advisor advice-ref="tx" pointcut-ref="p"/> </aop:config> </beans> 3.实体类与映射文件(同一包下) 假设实体类叫User.java, 放在domain包下,并在domain包下为User.java编写映射文件User.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="A"> <!--resultType="User" 用的是mybati配置文件中配置的别名(看第4步中), 也可以用别的类型,表示你希望mybatis将查出来的结果封装成什么类型返回给你,看你的实际需要--> <select id="selectUserById" parameterType="int" resultType="User"> select * from users where id=#{id} </select> </mapper> namespace叫什么都行, 但是一定要唯一,不能跟别的实体类一样 很多网上都是错误的,说namespace映射的是Dao,其实不是,namespace叫什么都行 4.mybati配置文件,类路径下 <?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> <!--alias="User"是给type里的类起一个别名,User.xml的resultType就是使用的这个别名,可以任意,但也要唯一--> <typeAlias type="domain.User" alias="User"/> </typeAliases> <mappers> <mapper resource="domain/User.xml"/> </mappers> </configuration> 5.mybatis没有hibernate的showsql功能, 要显示sql语句,可以添加log4j.properties文件,配置如下: log4j.rootLogger=info, Console #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n log4j.logger.java.sql.ResultSet=info log4j.logger.org.apache=info log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG 直接放到类路径下就可以直接使用, 不用在任何地方配置加载此文件!
6.简单查询
@Repository("demoDao") public class DemoDaoImpl extends SqlSessionDaoSupport implements DemoDao{ public User getUserById(int id) { return (User) this.getSqlSession().selectOne("A.selectUserById",1); } } A就是User.xml中的namespace, selectUserById就是<select.../>中的id A.selectUserById就唯一确定了这条sql语句select * from users where id=#{id} 1就是传入的参数,执行之后log4j会在控制台输出如下: 2013-08-28 17:19:16,896 [http-8888-2] DEBUG [java.sql.PreparedStatement] - ==> Executing: select * from users where id=? 2013-08-28 17:19:16,897 [http-8888-2] DEBUG [java.sql.PreparedStatement] - ==> Parameters: 1(Integer) 其中的Parameters:1(Integer)是显示传入的参数