springMVC+spring+Mybatis

一、小工具:

fastJson:温少的json小工具,打印非常好用

JsonView: json查看工具

d

二、项目配置

1.pom.xml

注意:spring包尽量置前,防止包依赖旧版本

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>sy.testmybatis</groupId>
	<artifactId>testmybatis</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>testmybatis Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.2.0.RELEASE</version>
		</dependency>
		<dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.17</version>
            <!-- <configuration>
                <testFailureIgnore>true</testFailureIgnore>
            </configuration> -->
        </dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>0.2.9</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.11</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>3.0-alpha-1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.26</version>
		</dependency>
	</dependencies>
	<build>
	</build>
</project>

2.web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
	<display-name>mybatis</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
	</context-param>
	<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集编码</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<listener>
		<description>spring监听器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 防止spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<!-- spring mvc servlet -->
	<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>spring mvc 配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>/index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 配置session超时时间,单位分钟 -->
	<session-config>
		<session-timeout>15</session-timeout>
	</session-config>
</web-app>

3.spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config.properties" />

	<!-- 自动扫描(自动注入) -->
	<context:component-scan base-package="sy.service" />

</beans>

4.spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
">

	<!-- JNDI方式配置数据源 -->
	<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="${jdbc_url}" />
		<property name="username" value="${jdbc_username}" />
		<property name="password" value="${jdbc_password}" />

		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />

		<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
		<property name="validationQuery" value="${validationQuery}" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />

		<!-- 监控数据库 -->
		<!-- <property name="filters" value="stat" /> -->
		<property name="filters" value="mergeStat" />
	</bean>

	<!-- myBatis文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
		<property name="mapperLocations" value="classpath:sy/mapping/*.xml" />
	</bean>

	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="sy.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 注解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

	<!-- 拦截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="append*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="repair" propagation="REQUIRED" />
			<tx:method name="delAndRepair" propagation="REQUIRED" />

			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />
			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="transactionPointcut" expression="execution(* sy.service..*Impl.*(..))" />
		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
	</aop:config>

	<!-- 配置druid监控spring jdbc -->
	<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
	</bean>
	<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">
		<property name="patterns">
			<list>
				<value>sy.service.*</value>
			</list>
		</property>
	</bean>

	<aop:config>
		<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />
	</aop:config>
</beans>

5. spring-mvc.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:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    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.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
	<context:component-scan base-package="sy.controller" />

	<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
	<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
			</list>
		</property>
	</bean>

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
			</list>
		</property>
	</bean>

	<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />

	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding">
			<value>UTF-8</value>
		</property>
		<property name="maxUploadSize">
			<value>32505856</value> <!-- 上传文件大小限制为31M,31*1024*1024 -->
		</property>
		<property name="maxInMemorySize">
			<value>4096</value>
		</property>
	</bean>
</beans>

三、mybatis使用:crud操作及如何获取关联对象(用户和角色通过中间表多对多关联)

1.mybatis generator使用

1.1 generator配置:

<generatorConfiguration>
	<!-- 数据库驱动包位置 -->
	<!-- <classPathEntry location="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
	<classPathEntry location="C:\oracle\product\10.2.0\db_1\jdbc\lib\ojdbc14.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!-- 数据库链接URL、用户名、密码 -->
		<!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> -->
		<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="sy.model" targetProject="D:\study\mybatis\src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- 生成的映射文件包名和位置 -->
		<sqlMapGenerator targetPackage="sy.mapping" targetProject="D:\study\mybatis\src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="sy.dao" targetProject="D:\study\mybatis\src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!-- 要生成那些表(更改tableName和domainObjectName就可以) -->

		<table tableName="trole" domainObjectName="Role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="tuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
		<table tableName="tuser_trole" domainObjectName="UserRole" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
	</context>
</generatorConfiguration>

1.2 generater 生成

 java -jar mybatis-generator-core-1.3.2.jar -configfile generator.xml -overwrite

2.映射文件UserMapper.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="sy.dao.UserMapper">
	<resultMap id="BaseResultMap" type="sy.model.User">
		<id column="ID" property="id" jdbcType="VARCHAR" />
		<result column="CREATEDATETIME" property="createdatetime" jdbcType="TIMESTAMP" />
		<result column="MODIFYDATETIME" property="modifydatetime" jdbcType="TIMESTAMP" />
		<result column="NAME" property="name" jdbcType="VARCHAR" />
		<result column="PWD" property="pwd" jdbcType="VARCHAR" />
		<result column="CREATE_TIME" property="createTime" jdbcType="DATE" />
		<result column="UPDATE_TIME" property="updateTime" jdbcType="DATE" />
	</resultMap>
	<sql id="Base_Column_List">
		ID, CREATEDATETIME, MODIFYDATETIME, NAME, PWD, CREATE_TIME, UPDATE_TIME
	</sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
		select
		<include refid="Base_Column_List" />
		from tuser
		where ID = #{id,jdbcType=VARCHAR}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
		delete from tuser
		where ID = #{id,jdbcType=VARCHAR}
	</delete>
	<insert id="insert" parameterType="sy.model.User">
		insert into tuser (ID, CREATEDATETIME, MODIFYDATETIME,
		NAME, PWD, CREATE_TIME,
		UPDATE_TIME)
		values (#{id,jdbcType=VARCHAR}, #{createdatetime,jdbcType=TIMESTAMP}, #{modifydatetime,jdbcType=TIMESTAMP},
		#{name,jdbcType=VARCHAR}, #{pwd,jdbcType=VARCHAR}, #{createTime,jdbcType=DATE},
		#{updateTime,jdbcType=DATE})
	</insert>
	<insert id="insertSelective" parameterType="sy.model.User">
		insert into tuser
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				ID,
			</if>
			<if test="createdatetime != null">
				CREATEDATETIME,
			</if>
			<if test="modifydatetime != null">
				MODIFYDATETIME,
			</if>
			<if test="name != null">
				NAME,
			</if>
			<if test="pwd != null">
				PWD,
			</if>
			<if test="createTime != null">
				CREATE_TIME,
			</if>
			<if test="updateTime != null">
				UPDATE_TIME,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id,jdbcType=VARCHAR},
			</if>
			<if test="createdatetime != null">
				#{createdatetime,jdbcType=TIMESTAMP},
			</if>
			<if test="modifydatetime != null">
				#{modifydatetime,jdbcType=TIMESTAMP},
			</if>
			<if test="name != null">
				#{name,jdbcType=VARCHAR},
			</if>
			<if test="pwd != null">
				#{pwd,jdbcType=VARCHAR},
			</if>
			<if test="createTime != null">
				#{createTime,jdbcType=DATE},
			</if>
			<if test="updateTime != null">
				#{updateTime,jdbcType=DATE},
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="sy.model.User">
		update tuser
		<set>
			<if test="createdatetime != null">
				CREATEDATETIME = #{createdatetime,jdbcType=TIMESTAMP},
			</if>
			<if test="modifydatetime != null">
				MODIFYDATETIME = #{modifydatetime,jdbcType=TIMESTAMP},
			</if>
			<if test="name != null">
				NAME = #{name,jdbcType=VARCHAR},
			</if>
			<if test="pwd != null">
				PWD = #{pwd,jdbcType=VARCHAR},
			</if>
			<if test="createTime != null">
				CREATE_TIME = #{createTime,jdbcType=DATE},
			</if>
			<if test="updateTime != null">
				UPDATE_TIME = #{updateTime,jdbcType=DATE},
			</if>
		</set>
		where ID = #{id,jdbcType=VARCHAR}
	</update>
	<update id="updateByPrimaryKey" parameterType="sy.model.User">
		update tuser
		set CREATEDATETIME = #{createdatetime,jdbcType=TIMESTAMP},
		MODIFYDATETIME = #{modifydatetime,jdbcType=TIMESTAMP},
		NAME = #{name,jdbcType=VARCHAR},
		PWD = #{pwd,jdbcType=VARCHAR},
		CREATE_TIME = #{createTime,jdbcType=DATE},
		UPDATE_TIME = #{updateTime,jdbcType=DATE}
		where ID = #{id,jdbcType=VARCHAR}
	</update>

	<!-- N+1查询问题 -->
	<select id="selectUserRole" parameterType="java.lang.String" resultType="sy.model.UserRole">
		SELECT
		tuser_trole.ID,
		tuser_trole.ROLE_ID,
		tuser_trole.USER_ID
		FROM
		tuser_trole
		where tuser_trole.user_id=#{id,jdbcType=VARCHAR}
	</select>

	<resultMap type="sy.model.User" id="userResultMap" extends="BaseResultMap">
		<collection property="userRoles" column="ID" javaType="list" select="selectUserRole" />
	</resultMap>

	<select id="getAll" resultMap="userResultMap">
		select ID, CREATEDATETIME, MODIFYDATETIME, NAME, PWD from tuser
	</select>

	<resultMap type="sy.model.User" id="userResultMap2" extends="BaseResultMap">
		<collection property="userRoles" javaType="list" ofType="sy.model.UserRole">
			<id property="id" column="userrole_id" />
			<result column="role_id" property="roleId" />
			<result property="userId" column="user_id" />
		</collection>
	</resultMap>
	<select id="getAll2" resultMap="userResultMap2">
		SELECT
		tuser.ID,
		tuser.CREATEDATETIME,
		tuser.MODIFYDATETIME,
		tuser.`NAME`,
		tuser.PWD,
		tuser.CREATE_TIME,
		tuser.UPDATE_TIME,
		tuser_trole.ID userrole_id,
		tuser_trole.ROLE_ID role_id,
		tuser_trole.USER_ID user_id
		FROM
		tuser
		JOIN tuser_trole ON tuser.ID = tuser_trole.USER_ID
	</select>

	<!-- 通过中间表对象UserRole获取角色 -->
	<resultMap type="sy.model.User" id="userResultMap3" extends="BaseResultMap">
		<collection property="userRoles" javaType="list" ofType="sy.model.UserRole">
			<id property="id" column="userrole_id" />
			<result column="role_id" property="roleId" />
			<result property="userId" column="user_id" />

			<association property="role" javaType="sy.model.Role">
				<id property="id" column="role_id" />
				<result property="text" column="role_text" />
			</association>
		</collection>
	</resultMap>

	<select id="getAll3" resultMap="userResultMap3">
		SELECT
		tuser.ID,
		tuser.CREATEDATETIME,
		tuser.MODIFYDATETIME,
		tuser.`NAME`,
		tuser.PWD,
		tuser.CREATE_TIME,
		tuser.UPDATE_TIME,
		tuser_trole.ID userrole_id,
		tuser_trole.ROLE_ID role_id,
		tuser_trole.USER_ID user_id,
		trole.ID role_id,
		trole.TEXT role_text
		FROM
		tuser
		JOIN tuser_trole ON tuser.ID = tuser_trole.USER_ID
		JOIN trole ON tuser_trole.ROLE_ID = trole.ID
	</select>

	<!-- 直接获取角色,中间表对象UserRole -->
	<resultMap type="sy.model.User" id="userResultMap4" extends="BaseResultMap">
		<collection property="roles" javaType="list" ofType="sy.model.Role">
			<id property="id" column="role_id" />
			<result property="text" column="role_text" />
		</collection>
	</resultMap>

	<select id="getAll4" resultMap="userResultMap4">
		SELECT
		tuser.ID,
		tuser.CREATEDATETIME,
		tuser.MODIFYDATETIME,
		tuser.`NAME`,
		tuser.PWD,
		tuser.CREATE_TIME,
		tuser.UPDATE_TIME,
		tuser_trole.ID userrole_id,
		tuser_trole.ROLE_ID,
		tuser_trole.USER_ID,
		trole.ID role_id,
		trole.TEXT role_text
		FROM
		tuser
		JOIN tuser_trole ON tuser.ID = tuser_trole.USER_ID
		JOIN trole ON tuser_trole.ROLE_ID = trole.ID
	</select>

</mapper>
时间: 2024-10-12 18:52:09

springMVC+spring+Mybatis的相关文章

Idea SpringMVC+Spring+MyBatis+Maven调整【转】

Idea SpringMVC+Spring+MyBatis+Maven整合 创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetype,然后选中下方列表中的webapp,然后点击Next 在GroupId和ArtifactId中填入指定内容,点击Next 直接点Next 输入项目名称,Finish Idea会自动开始下载所依赖的包,等待其完成. 项目结构 项目刚建好的时候是没有这些文件的,所以自己手动创建缺少的文件夹(包) 创建完后

SpringMVC+Spring+Mybatis(SSM~Demo) 【转】

SpringMVC+Spring+Mybatis 框架搭建 整个Demo的视图结构: JAR: 下载地址:http://download.csdn.net/detail/li1669852599/8546059 首先,我是使用MyEclipse工具做的这个例子,整合了Sping 3 .Spring MVC 3 .MyBatis框架,演示数据库采用MySQL数据库.例子中主要操作包括对数据的添加(C).查找(R).更新(U).删除(D).我在这里采用的数据库连接池是来自阿里巴巴的Druid,至于D

Maven 搭建SpringMvc+Spring+Mybatis详细记录

总觉得,看比人写的总是那么好,每次搭建框架时都会找博客,找教程来跟着一步一步走,虽然很快搭建成功了,但是经常情况是我并不知道我干了什么,也不记得具体步骤,到底为什么要这么做,今天我详细记录了一下自己搭建的过程,并且尽量理解每一步干什么. SSM框架当下比较流行,我也是用这个框架来作为记录,尝试详细地记录下每一个步骤,学习,不要怕开头难. 一.创建一个新的Maven项目 1. new -> Maven -> Maven Project 选择webapp工程. 2.maven项目建好以后,工程目录

maven搭建springmvc+spring+mybatis实例

最近做了个maven管理的springmvc+spring+mybatis,还用到了阿里巴巴的 fastjson和druid连接池,配置文件如下 pom.xml文件 [html] view plaincopy <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:

SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置

一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不能用,log也不知道能不能用,`(*∩_∩*)′哈哈,有点不负责任...... 直接上代码: 使用的eclipse和eclipse自带的maven,参考了网上的资料,有些代码是拷贝的,不过都自己测试过了.嗯,可以跑起来... 先上项目结构: 新建maven项目,选择web,然后配置pom: <pro

springmvc+spring+mybatis+maven项目构建

1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropins\maven中, 2. 新建文件:maven.link填入如下内容:path=D:/Program Files (x86)/myEclipse10/MyEclipse Blue Edition 10/dropins/maven 3.重启myeclipse插件安装成功. 4.在myeclipse10

springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解

@[email protected] 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) mybatis学习笔记(六) -- maven+spring+mybatis从零开始搭建整合详细过程(下) springmvc学习笔记(一) -- 从零搭建,基础入门 这一篇,在这些练习的基础上,将它们整合在一起! 搭建步骤如下 一.新建maven项目,配置环境,测试是否配置成

SSM(springmvc+spring+mybatis)整合过程

问题?SSM(springmvc+spring+mybatis)整合过程 一.SSM框架比较S2SH框架的优缺点比较 站在个人使用这两个框架的观点儿上来说的话,我觉得这个不能定死的说,常用的S2SH,SpringMVC等,还可以考虑jFinal. 首先说S2SH,这个是用的比较多的,特别是在中小型项目中,针对大型项目就不行了,是Struts和hibernate过于庞大,过于重量级,项目大了之后配置文件多的也是麻烦事,配置文件多了之后一方面不好操作,另外比较蛋疼的是没法断点调试,特别是对底层数据库

ztree使用系列三(ztree与springmvc+spring+mybatis整合实现增删改查)

在springmvc+spring+mybatis里整合ztree实现增删改查,上一篇已经写了demo,下面就只贴出各层实现功能的代码: Jsp页面实现功能的js代码如下: <script> //用于捕获分类编辑按钮的 click 事件,并且根据返回值确定是否允许进入名称编辑状态 function beforeEditName(treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.

spring+websocket整合(springMVC+spring+MyBatis即SSM框架)

spring4.0以后加入了对websocket技术的支持,撸主目前的项目用的是SSM(springMVC+spring+MyBatis)框 架,所以肯定要首选spring自带的websocket了,好,现在问题来了,撸主在网上各种狂搜猛找,拼凑了几个自称是 spring websocket的东东,下来一看,废物,其中包括从github上down下来的.举个例子,在搭建过程中有个问题, 撸主上谷歌搜索,总共搜出来三页结果共30条左右,前15条是纯英文的  后15条是韩语和日语,而这30条结果都不