您现在的位置是:主页 > news > 中企动力网站建设方案/百度推广网址是多少
中企动力网站建设方案/百度推广网址是多少
admin2025/5/24 12:51:37【news】
简介中企动力网站建设方案,百度推广网址是多少,专业网站建设定制公司,阿里巴巴国际网站建设一、VO 与 resultMap resultMap 由user_id字段与pojo属性不一致时引出的resultMap问题:使用resultTypeorder 不能找到订单中user对象的id原因:订单中属性名为userid, 数据库中字段名为user_id 解决:1)修改订单属性…
中企动力网站建设方案,百度推广网址是多少,专业网站建设定制公司,阿里巴巴国际网站建设一、VO 与 resultMap resultMap
由user_id字段与pojo属性不一致时引出的resultMap问题:使用resultTypeorder 不能找到订单中user对象的id原因:订单中属性名为userid, 数据库中字段名为user_id 解决:1)修改订单属性…
一、VO 与 resultMap
resultMap
由user_id字段与pojo属性不一致时引出的resultMap问题:使用resultType=order 不能找到订单中user对象的id原因:订单中属性名为userid, 数据库中字段名为user_id 解决:1)修改订单属性名 或 修改数据库字段名 使二者统一2)使用别名。 对属性名使用别名 别名值为数据库字段名3)使用resultMap 手动完成属性到字段的映射
查询所有订单:<!-- 方式二:使用别名 --><select id="queryOrderAll" resultType="mybatis.pojo.Order"> SELECT `id`,`user_id` AS userId, 对字段名起别名 起为属性名 `number`,`createtime`,`note` FROM`order`</select><!-- 方式三:resultMap:对指定的pojo手动完成 属性名 到 字段名的映射type:被指定的pojoid:resultMap的唯一标识--><resultMap type="mybatis.pojo.Order" id="orderResultMap"> property:pojo属性名 column:字段名 <result property="userId" column="user_id"/></resultMap><select id="queryOrderAll" resultMap="orderResultMap"> resultMap:被指定的resultMap的 id值SELECT *FROM `order`</select>
VO :value object值对象
<!--
根据QueryVo查询 设置查询参数为int类型 返回值为User类
多层类对象查询
-->
<select id="queryUserByQueryVo" parameterType="mybatis.pojo.QueryVo" resultType="mybatis.pojo.User">SELECT *FROM`user` where `id` = #{user.id}
</select>
二、 动态sql
where标签、If标签使用
where标签 : 1)可自动添加where 不需要where 1=12)可处理第一个AND
if标签:可根据传入参数 自动判断并添加 条件语句 <!--
根据条件查询user :根据性别 和 姓名模糊查询
-->
<select id="queryUserByWhere" parameterType="user" resultType="User">SELECT * FROM `user` <!-- where `sex` = #{sex} AND username LIKE '%${username}%' --><where><if test="sex != null">AND `sex` = #{sex}</if><if test="username != null and username != ''">AND username LIKE '%${username}%'</if></where></select>
foreach标签
适用场景:需要遍历条件时 如:SELECT * FROM `user` WHERE id IN (1,10,29,32,40,44);步骤:1)设置vo,设置一个集合属性,用于存放条件2)sql配置:使用foreach标签 拼接条件语句3)测试输出
一、VO
二、sql配置:
foreach标签 : 遍历查询1)collection:参数名称,根据Mapper接口的参数名确定2)item:参数调用名称,通过此属性来获取集合单项的值3)open:相当于prefix,即在循环前添加前缀4)close:相当于suffix,即在循环后添加后缀5)separator:分隔符,每次循环完成后添加此分隔符 6)index:索引、下标<!-- 根据ids查询用户 -->
<select id="queryUserByIds" parameterType="mybatis.pojo.QueryVo" resultType="mybatis.pojo.User">select * from `user`<where><foreach collection="ids" item="ids" open="id IN (" close=")" separator=",">#{ids}</foreach></where>
</select>
三、测试:
@Testpublic void testQueryUserByIds(){//得到sqlSessionSqlSession sqlSession = Utils.getSqlSessionFactory().openSession();//在底层生成接口的代理实现类UserDao userDao = sqlSession.getMapper(UserDao.class);QueryVo queryVo = new QueryVo();List<Integer> ids = new ArrayList<Integer>();ids.add(1);ids.add(22);ids.add(30);ids.add(44);queryVo.setIds(ids);List<User> list = userDao.queryUserByIds(queryVo);for (User u : list) {System.out.println(u);}sqlSession.close();
}
sql标签: 可对sql片段进行提取封装
引用sql片段的标签: <include refid=""></include><!-- 封装sql片段,设置id作为标识 -->
<sql id="orderFields">`id`,`user_id`, `number`, `createtime`, `note`
</sql><!-- 查询 -->
<select id="queryOrderAll" resultType="mybatis.pojo.Order">SELECT <include refid="orderFields"></include> <!-- 使用被封装的sql片段--> FROM `order`
</select>
三、一对一,一对多关联查询
1)一对一关联查询:
步骤:一、在pojo中声明另一pojo为属性二、使用resultMap配置sql<association property="属性名" javaType="类全路径">三、测试接口方法
一、在pojo中声明另一pojo为属性
二、使用resultMap配置sql
resultMap中 一的配置<association property="属性名" javaType="类全路径">
<!-- 一对一表查询 -->
<!-- 查询订单及订单所有者 -->
<resultMap type="mybatis.pojo.Order" id="order_user_mapper"><id property="id" column="id"/><result property="userId" column="user_id"/><result property="number" column="number"/><result property="createtime" column="createtime"/><result property="note" column="note"/><!-- user属性映射配置 --><association property="user" javaType="mybatis.pojo.User"><id property="id" column="user_id"/><result property="username" column="username"/><result property="address" column="address"/></association>
</resultMap>
<select id="queryOrderUser" resultMap="order_user_mapper">SELECT o.* ,u.`username`,u.`address`FROM`order` oJOIN `user` u ON u.`id` = o.`user_id`;
</select>
三、测试接口方法
@Testpublic void testQueryOrderUser() {//创建sqlSessionSqlSession session = Utils.getSqlSessionFactory().openSession();//底层创建接口的代理实现类OrderMapper orderMapper = session.getMapper(OrderMapper.class);//执行接口中查询方法List<Order> list = orderMapper.queryOrderUser();for (Order order : list) {System.out.println("\n" + order);System.out.println("订单所有者:"+order.getUser());}//关闭sessionsession.close();}
2)一对多关联查询
步骤:一、在pojo中声明另一pojo为集合属性二、使用resultMap配置sql<collection property="属性名" javaType="List" ofType="类全路径">三、测试接口方法需要双重循环输出
一、在pojo中声明另一pojo为属性(多的一方为集合类型)**
二、使用resultMap配置sql
resultMap中 多的配置<collection property="属性名" javaType="List" ofType="类全路径">
<resultMap type="mybatis.pojo.User" id="User_Order_Mapper"><id property="id" column="id"/><result property="username" column="username"/><result property="birthday" column="birthday"/><result property="sex" column="sex"/><result property="address" column="address"/><collection property="orderList" javaType="List" ofType="mybatis.pojo.Order"><id property="id" column="oid"/><result property="userId" column="user_id"/><result property="number" column="number"/><result property="createtime" column="createtime"/><result property="note" column="note"/></collection>
</resultMap>
<select id="queryUserOrder" resultMap="User_Order_Mapper"> SELECT u.`id`,u.`username`,u.`birthday`,u.`sex`,u.`address` ,o.`id` oid,o.`user_id`,o.`number`,o.`createtime`,o.`note` FROM`user` uJOIN `order` o ON u.`id` = o.`user_id`;
</select>
三、测试接口方法
@Test
public void testQueryUserOrder(){//得到sqlSessionSqlSession sqlSession = Utils.getSqlSessionFactory().openSession();//在底层生成接口的代理实现类UserDao userDao = sqlSession.getMapper(UserDao.class);List<User> list = userDao.queryUserOrder();//一对多中,需要双重循环输出for (User u : list) { //遍历用户System.out.println(u);for (Order order : u.getOrderList()) { //遍历用户订单System.out.println(order);}}sqlSession.close();
}
四、Mybatis整合spring
整合思路
1、数据库、连接池、事务管理在spring容器来完成。
2、SqlSessionFactory对象放进spring容器中,作为单例存在。
3、从spring容器中获得sqlsession对象。(传统dao的开发方式中)
4、从spring容器中获得mapper的代理对象。(Mapper代理方式中)
整合步骤
1. 创建一个java工程。
2. 导入jar包。(课前资料中mybatis与spring整合所有包)
3. 配置sqlmapConfig.xml
4. 编写Spring的配置文件(applicationContext.xml)1) 数据库连接及连接池2) sqlsessionFactory对象,配置到spring容器中3) 编写Spring的配置文件
5. jdbc.properties
6. log4j.properties
sqlmapConfig.xml不作配置
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 加载配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 数据库连接池 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><!-- 连接池的最大数据库连接数 --><property name="maxActive" value="10" /><!-- 最大空闲数 --><property name="maxIdle" value="5" /></bean><!-- 配置SqlSessionFactory,配置三个属性加载配置文件加载数据库源设置别名包扫描 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:SqlMapConfig.xml"></property><property name="dataSource" ref="dataSource"></property><property name="typeAliasesPackage" value="mybatis.pojo"></property></bean><!-- 动态代理开发,第一种方式 -MapperFactoryBean<bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"><property name="sqlSessionFactory" ref="sqlSessionFactory" /></bean>--><!-- 用户动态代理单个接口配置<bean parent="baseMapper"><property name="mapperInterface" value="mybatis.mapper.UserMapper" /></bean>--><!-- 动态代理开发,第二种方式,包扫描器(推荐使用) --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- basePackage:配置映射包装扫描,多个包时用","或";"分隔 --><property name="basePackage" value="mybatis.mapper" /></bean></beans>
Test
/*** 需要初始化得到spring容器,从spring容器中得到Mapper底层代理实现类* @author 一万年行不行**/
public class UserMapperTest {//定义applicationContext 初始化得到spring容器private ApplicationContext applicationContext;@Beforepublic void init() {applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");}@Testpublic void testqueryUserById() {//通过spring容器得到Mapper底层代理实现类UserMapper userMapper = applicationContext.getBean(UserMapper.class);//输出查询结果System.out.println(userMapper.queryUserById(29));}
}
传统Dao开发
1. 在SqlMapConfig.xml加载user.xml
2. 编写UserDaoImpl实现类,关键是继承SqlSessionDaoSupport
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {…}