我是靠谱客的博主 忧心鸡,这篇文章主要介绍mybatis嵌套查询和嵌套结果 SSM 框架的集成,现在分享给大家,希望可以做个参考。

文章目录

    • mybatis 的运用
      • 1.mybatis的了解
        • 1.1 mybatis是ORM持久层框架,MyBatis是针对数据库交互的一个辅助框架,也是对jdbc做了的简单封装,以xml配置代替Java代码来管理数据库的交互细节.
        • 1.2 MyBatis可以使用简单的**XML或注解**用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录.
      • 2.mybatis的映射器
        • 2.1 SpringAOP 面向切面编程 --底层代理模式(额外产出一些子类)
        • 2.2高级查询
          • 2.2.1 like(模糊查询) 使用concat
          • 2.2.2 注意转义符的运用
      • 3.mybatis的嵌套结果 嵌套查询
        • 3.1 多对一的 嵌套结果 嵌套查询
        • 3.2 一对多的嵌套结果 嵌套查询 (limit 分页用于嵌套查询,不能用于嵌套结果)
      • 4.SSM 框架的搭建
        • 4.1普通的web项目
          • 4.1.1 结构图
          • 4.1.2 applicationContext.xml 的配置
          • 4.1.3 applicationContext-mvc.xml 的配置
          • 4.1.4web.xml的配置

mybatis 的运用

1.mybatis的了解

1.1 mybatis是ORM持久层框架,MyBatis是针对数据库交互的一个辅助框架,也是对jdbc做了的简单封装,以xml配置代替Java代码来管理数据库的交互细节.

1.2 MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录.

2.mybatis的映射器

2.1 SpringAOP 面向切面编程 --底层代理模式(额外产出一些子类)

MyBatis基于动态代理机制,让我们无需再编写Dao的实现。

传统Dao接口,现在名称统一以Mapper结尾,还有我们映射器配置文件要和映射器在同一个包.

IDeptDao---->DeptMapper.java—DeptMapper.xml(namespace直接写DeptMapper.java的全限定名)

2.2高级查询

2.2.1 like(模糊查询) 使用concat
复制代码
1
2
3
4
<if test="name!=null"> and name like concat('%',#{name},'%') </if>
2.2.2 注意转义符的运用
复制代码
1
2
3
4
5
6
7
8
9
10
11
方案1:转义符号 <if test="maxSalePrice != null"> <!--and saleprice &lt;= #{maxSalePrice} </if> 方案2:cdata 的区域编译的时候便不会读取 xml <![CDATA[ and saleprice <= #{maxSalePrice} ]]>

3.mybatis的嵌套结果 嵌套查询

3.1 多对一的 嵌套结果 嵌套查询

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
嵌套查询和嵌套结果 <mapper namespace="cn.itsource._06_manytoone1.EmployeeMapper"> 嵌套结果 <!--<resultMap id="employeeMap" type="employee"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="age" column="age"></result> <association property="department" javaType="Department"> <id property="id" column="did"></id> <result property="name" column="dname"></result> </association> </resultMap> <select id="findAll" resultMap="employeeMap"> SELECT e.id, e. NAME, e.age, d.id did, d. NAME dname FROM t_employee e JOIN t_department d ON e.did = d.id </select>--> 嵌套查询 <resultMap id="employeeMap" type="employee"> <id property="id" column="id"></id> <result property="name" column="name"></result> <result property="age" column="age"></result> <association property="department" javaType="Department" column="did" select="findDeptById"> </association> </resultMap> <select id="findAll" resultMap="employeeMap"> SELECT e.id, e. NAME, e.age, e.did FROM t_employee e </select> <select id="findDeptById" parameterType="long" resultType="department"> select * from t_department where id=#{id} </select> </mapper>

3.2 一对多的嵌套结果 嵌套查询 (limit 分页用于嵌套查询,不能用于嵌套结果)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
嵌套结果 <!--<resultMap id="deptMap" type="department"> <id property="id" column="id"></id> <result property="name" column="name"></result> <collection property="employees" javaType="arraylist" ofType="Employee" > <id property="id" column="eid"></id> <result property="name" column="ename"></result> <result property="age" column="eage"></result> </collection> </resultMap> <select id="loadAll" resultMap="deptMap"> SELECT d.id, d. NAME, e.id eid, e. NAME ename, e.age eage FROM t_department d JOIN t_employee e ON d.id = e.did </select>--> 嵌套查询 <resultMap id="deptMap" type="department"> <id property="id" column="id"></id> <result property="name" column="name"></result> <collection property="employees" column="id" javaType="arraylist" ofType="Employee" select="findEmptById"></collection> </resultMap> <select id="loadAll" resultMap="deptMap"> select t.id,t.name from t_department t limit 0,1 </select> <select id="findEmptById" parameterType="long" resultType="employee"> select * from t_employee where did =#{id} </select>

4.SSM 框架的搭建

4.1普通的web项目

4.1.1 结构图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M1vAikJY-1574910242562)(C:UsersAdministratorAppDataRoamingTyporatypora-user-images1574909718440.png)]

4.1.2 applicationContext.xml 的配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<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" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置整合mybatis过程 --> <!-- 1.配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties" /> <context:component-scan base-package="cn.itsource.ssm.service"/> <!-- 2.数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 配置连接池属性 --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml" /> <!-- 配置mybatis 类型别名 --> <property name="typeAliasesPackage"> <value> cn.itsource.ssm.domain cn.itsource.ssm.query </value> </property> </bean> <!-- 4.配置扫描mapper接口包,动态实现mapper接口,注入到spring容器中 扫描 MapperScannerConfigurer--> <!--一劳永逸--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.itsource.ssm.mapper"></property> </bean> <!--事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置基于注解的声明式事务 @Transactional--> <tx:annotation-driven transaction-manager="transactionManager" />
4.1.3 applicationContext-mvc.xml 的配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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="cn.itsource.ssm.controller" /> <!--静态资源处理--> <mvc:default-servlet-handler /> <!--识别@requestMapper等注解支持--> <mvc:annotation-driven /> <!--配置视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
4.1.4web.xml的配置
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--配置spring-mvc 的核心控制器--> <!--方案1:独立容器方案,spring和springmvc都有自己context--> <!--1.1 spring容器初始化--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--1.2 配置springmvc--> <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:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--/*会拦截所有包括jsp--> <!--/会拦截所有但是不包含jsp--> <url-pattern>/</url-pattern> </servlet-mapping> <!-- post请求乱码解决 get请求实在tomcat猫中配置 --> <filter> <filter-name>EncodingFilter</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>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

最后

以上就是忧心鸡最近收集整理的关于mybatis嵌套查询和嵌套结果 SSM 框架的集成的全部内容,更多相关mybatis嵌套查询和嵌套结果内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部