mybatis如何在页面画出tp框架修改密码码框架

用jsp写个网站,后台数据库层用mybatis 在画面上操作增删改,框架可以用strus_百度知道MyBatis框架(22)
一对一查询
根据上面我们分析的订单商品数据模型(链接:12.订单商品数据模型-分析思路.txt),我们来写一下有关一对一的查询,分别使用了resultType和resultMap指定输出参数类型
1.一对一查询使用resultType指定输出参数类型
查询订单信息,关联查询创建订单的用户信息
1.2resultType
1.2.1sql语句
确定查询的主表:订单表
确定查询的关联表:用户表
关联查询使用内链接?还是外链接?
由于orders表中有一个外键(user_id),通过外键关联查询用户表只能查询出一条记录,可以使用内链接。
USER.username,
USER.address
WHERE orders.user_id = user.id
1.2.2创建pojo
将上边sql查询的结果映射到pojo中,pojo中必须包括所有查询列名。
下面写Orders.java对映数据库表单
package cn.edu.hpu.mybatis.PO;
import java.util.D
public class Orders {
private Integer userId;
//get和set方法省略...
发现原始的Orders.java不能映射全部字段,需要新创建的pojo。
创建 一个pojo继承包括查询字段较多的po类。
package cn.edu.hpu.mybatis.PO;
//Orders订单的拓展类
//通过此类映射订单和用户查询的结果,让此类继承包括字段较多的pojo类
public class OrdersCustom extends Orders{
//添加用户属性
//get和set方法省略...
1.2.3mapper.xml
OrdersCustomMapper.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&&
&!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离
注意:使用mapper代理方法开发,namespace有特殊重要的作用 --&
&mapper namespace=&cn.edu.hpu.mybatis.mapper.OrdersCustomMapper&&
&!-- 查询订单关联查询用户信息 --&
&select id=&findOrdersUser& resultType=&cn.edu.hpu.mybatis.PO.OrdersCustom&&
USER.username,
USER.address
WHERE orders.user_id = user.id
&/mapper&在SqlMapConfig.xml中我们扫描了mapper包所有的Mapper映射文件,所以这里就不在SqlMapConfig.xml中单独加载Mapper映射文件了。
1.2.4mapper.java
package cn.edu.hpu.mybatis.
import java.util.L
import cn.edu.hpu.mybatis.PO.OrdersC
//订单mapper
public interface OrdersCustomMapper {
//查询订单关联查询用户信息
public List&OrdersCustom& findOrdersUser() throws E
public void testFindOrdersUser() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建代理对象
OrdersCustomMapper ordersMapperCustom=sqlSession.getMapper(OrdersCustomMapper.class);
//调用mapper的方法
List&OrdersCustom& list=ordersMapperCustom.findOrdersUser();
for (int i = 0; i & list.size(); i++) {
OrdersCustom
oc=list.get(i);
System.out.println(oc.getUsername()+&|&+oc.getSex()+&|&+oc.getAddress());
sqlSession.close();
测试结果:
张三|男|河南焦作
张三|男|河南焦作
刘莉莉|女|山东威
输出日志:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection .
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@1493102]
DEBUG [main] - ==&
Preparing: SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id
DEBUG [main] - ==& Parameters:
DEBUG [main] - &==
2.一对一查询使用resultMap指定输出参数类型
2.1sql语句
同resultType实现的sql
2.2使用resultMap映射的思路
使用resultMap将查询结果中的订单信息映射到Orders对象中,在orders类中添加User属性,将关联查询出来的用户信息映射到orders对象中的user属性中。
2.3需要Orders类中添加user属性
package cn.edu.hpu.mybatis.PO;
import java.util.D
public class Orders {
private Integer userId;
//用户信息
2.4mapper.xml
2.4.1定义resultMap
&!-- 订单查询关联用户的resultMap
将整个查询结果映射到cn.edu.hpu.mybatis.PO.Orders中--&
&resultMap type=&cn.edu.hpu.mybatis.PO.Orders& id=&OrdersUserResultMap&&
&!-- 配置映射的订单信息 --&
&!-- id:指定查询中的唯一标识,订单信息中的唯一标识,如果有多个列组成唯一标识,
那么就要配置多个id 。
column:订单信息的唯一标识列。
property:订单信息的唯一标识列所映射到Orderds中哪个属性--&
&id column=&id& property=&id&/&
&result column=&user_id& property=&userId&/&
&result column=&number& property=&number&/&
&result column=&creattime& property=&creattime&/&
&result column=&note& property=&note&/&
&!-- 配置关联用户的用户信息 --&
&!-- association:用于映射关联查询单个对象的信息。
property:要将关联查询的用户信息映射到Orders中哪个属性--&
&association property=&user& javaType=&cn.edu.hpu.mybatis.PO.User&&
&!-- id:关联查询用户的唯一标识
指定唯一标识用户信息的列
javaType:映射到User的哪个属性
&id column=&user_id& property=&id&/&
&result column=&username& property=&username&/&
&result column=&sex& property=&sex&/&
&result column=&address& property=&address&/&
&/association&
&/resultMap&
2.4.2statement定义
&!-- 查询订单关联查询用户信息,使用resultMap --&
&select id=&findOrdersUserResultMap& resultMap=&OrdersUserResultMap&&
USER.username,
USER.address
WHERE orders.user_id = user.id
2.4.3mapper.java
package cn.edu.hpu.mybatis.
import java.util.L
import cn.edu.hpu.mybatis.PO.O
import cn.edu.hpu.mybatis.PO.OrdersC
//订单mapper
public interface OrdersCustomMapper {
//查询订单关联查询用户使用resultMap
public List&Orders& findOrdersUserResultMap() throws E
public void testFindOrdersUserResultMap() throws Exception{
SqlSession sqlSession=sqlSessionFactory.openSession();
//创建代理对象
OrdersCustomMapper ordersMapperCustom=sqlSession.getMapper(OrdersCustomMapper.class);
//调用mapper的方法
List&Orders& list=ordersMapperCustom.findOrdersUserResultMap();
for (int i = 0; i & list.size(); i++) {
o=list.get(i);
User u=o.getUser();
System.out.println(u.getUsername()+&|&+u.getSex()+&|&+u.getAddress());
sqlSession.close();
测试结果:
张三|男|河南焦作
张三|男|河南焦作
刘莉莉|女|山东威海
日志输出:
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection .
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@147358f]
DEBUG [main] - ==&
Preparing: SELECT orders.*, USER.username, USER.sex, USER.address FROM orders, USER WHERE orders.user_id = user.id
DEBUG [main] - ==& Parameters:
DEBUG [main] - &==
3.resultType和resultMap实现一对一查询小结
实现一对一查询:
resultType:使用resultType实现较为简单,如果pojo中没有包括查询出来的列名,需要增加列名对应的属性,即可完成映射。
如果没有查询结果的特殊要求建议使用resultType。
resultMap:需要单独定义resultMap,实现有点麻烦,如果对查询结果有特殊的要求(比如pojo里面又含有pojo),使用resultMap可以完成将关联查询映射pojo的属性中。
resultMap可以实现延迟加载,resultType无法实现延迟加载。
转载请注明出处:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:410206次
积分:11318
积分:11318
排名:第900名
原创:734篇
转载:18篇
评论:232条
就职:北京科蓝软件上海分公司
职位:软件开发工程师
负责:网银系统开发与维护
专业:软件工程12级
Q Q :<font color="#ca490
阅读:1899
阅读:2502
文章:35篇
阅读:28552
文章:27篇
阅读:9081
文章:24篇
阅读:8561
文章:81篇
阅读:76095
文章:87篇
阅读:45424
文章:21篇
阅读:9908
文章:51篇
阅读:19238
文章:37篇
阅读:17461
文章:31篇
阅读:37545&&国之画&&&&&&
&& &&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!此第一次接触Mybatis框架确实是有点不适应,特别是刚从Hibernate框架转转型过来,那么为什么要使用Mybatis框架,Mybatis框架和Hibernate框架又有什么异同呢?
这个问题在我的另一篇blogs中有专门的讲解,今天我主要是带着大家来探讨一下如何简单的使用Mybatis这个框架
可能有的朋友知道,Mybatis中是通过配置文件来实现这个的,这里面有很多的东西,我们就一点一点的讲吧
我们想要配置成功,首要的就是jar包,先从官网下载相应的jar包作为程序的支撑
有了jar包之后我么就来看看我们程序的主要的搭建
具体类的内容如下
Student & &Class
public class Student {
//学生编号
//学生名称
//学生性别
public Student() {
public Student(String sname, String sex) {
this.sname =
this.sex =
public Integer getSid() {
public void setSid(Integer sid) {
this.sid =
public String getSname() {
public void setSname(String sname) {
this.sname =
public String getSex() {
public void setSex(String sex) {
this.sex =
Grade &Class
public class Grade {
//班级编号
//班级名称
//班级描述
public Grade() {
public Grade(Integer gid, String gname, String gdesc) {
this.gid =
this.gname =
this.gdesc =
public Integer getGid() {
public void setGid(Integer gid) {
this.gid =
public String getGname() {
public void setGname(String gname) {
this.gname =
public String getGdesc() {
public void setGdesc(String gdesc) {
this.gdesc =
接下来我么就要配置我们的主要配置文件了,主要是指定我们要连接的数据库和具体连接操作
Configuration.xml
&?xml version="1.0" encoding="UTF-8" ?&
Copyright 2009-2012 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
&!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"&
&configuration&
&settings&
&setting name="useGeneratedKeys" value="false"/&
&setting name="useColumnLabel" value="true"/&
&/settings&
&typeAliases&
&typeAlias alias="UserAlias" type="org.apache.plex_property.User"/&
&/typeAliases& --&
&environments default="development"&
&environment id="development"&
&transactionManager type="JDBC"&
&property name="" value=""/&
&/transactionManager&
&dataSource type="UNPOOLED"&
&property name="driver" value="oracle.jdbc.OracleDriver"/&
&property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/&
&property name="username" value="practice"/&
&property name="password" value="123"/&
&/dataSource&
&/environment&
&/environments&
&mapper resource="config/Student.xml"/&
&/mappers&
&/configuration&
其实最主要的是如下图所示
到这里为止,所有的准备工作基本上就已经是完成了
接下来,使用Mybatis框架来实现我们的具体操作&
1.查询所有学生信息
&因为Mybatis是属于一种半自动化的框架技术所以呢sql是我们手动书写的,这也是Mybatis的一大特点
我们可以写出具体的实体配置文件
Student.xml
&?xml version="1.0" encoding="UTF-8"?&
Copyright 2009-2012 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
&!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&
&mapper namespace="Student"&
&resultMap type="entity.Student" id="StudentResult"&
&id column="sid" jdbcType="INTEGER" property="sid"/&
&result column="sname" jdbcType="VARCHAR" property="sname"/&
&result column="sex" jdbcType="VARCHAR" property="sex"/&
&/resultMap&
&select id="selectAllStu"
resultMap="StudentResult"&
select * from Student
既然我们写了sql也指定了相应的实体类,那么我们到现在为止还并没有用到它,所以我们还需要在主配置文件中添加实体配置文件的引用
经过以上的步骤, 我们查询全部学生的配置文件基本上就已经完成了,现在我们来进行一道测试
* 1.1 查询所有的学生信息
public void OneTest() throws Exception{
//通过配置文件获取到数据库连接信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//通过配置信息构建一个SessionFactory工厂
SqlSessionFactory sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader);
//通过SessionFaction打开一个回话通道
SqlSession session = sqlsessionfactory.openSession();
//调用配置文件中的sql语句
List&Student& list = session.selectList("Student.selectAllStu");
//遍历查询出来的结果
for (Student stu : list) {
System.out.println(stu.getSname());
session.close();
执行之后的语句如下
&这样我们使用Mybatis查询所有学生信息就完成了
2.带条件查询动态Sql拼接
*1.2 带条件查询信息(动态Sql拼接)
public void selectAllStuByWhere() throws Exception{
//通过配置文件获取到数据库连接信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//通过配置信息构建一个SessionFactory工厂
SqlSessionFactory sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader);
//通过SessionFaction打开一个回话通道
SqlSession session = sqlsessionfactory.openSession();
//准备一个学生对象作为参数
Student student=new Student();
student.setSname("3");
//调用配置文件中的sql语句
List&Student& list = session.selectList("Student.selectAllStuByWhere",student);
//遍历查询出来的结果
for (Student stu : list) {
System.out.println(stu.getSname());
session.close();
小配置配置文件信息
&?xml version="1.0" encoding="UTF-8"?&
Copyright 2009-2012 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
&!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"&
&mapper namespace="Student"&
&resultMap type="entity.Student" id="StudentResult"&
&id column="sid" jdbcType="INTEGER" property="sid"/&
&result column="sname" jdbcType="VARCHAR" property="sname"/&
&result column="sex" jdbcType="VARCHAR" property="sex"/&
&/resultMap&
&!-- 简单查询所有信息 --&
&select id="selectAllStu"
resultMap="StudentResult"&
select sid,sname,sex,gid from Student
&!--动态拼接Sql
&select id="selectAllStuByWhere" parameterType="entity.Student"
resultMap="StudentResult"&
select sid,sname,sex,gid from Student where 1=1
&if test="sname!=null and !&&.equals(sname.trim())"&
and sname like '%'|| #{sname}|| '%' &!-- 模糊查询 --&
&!-- and sname = #{sname} --&
执行之后的结果就是
3.新增学生信息
* 1.3 新增学生信息
public void InsertStuInfo() throws Exception{
//通过配置文件获取配置信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//构建一个SessionFactory,传入配置文件
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
//获取session
SqlSession session = factory.openSession();
//准备参数对象
Student stu=new Student();
stu.setSname("巴黎的雨季");
stu.setSex("男");
//调用添加方法
int count = session.insert("Student.InsertStuInfo", stu);
if(count&0){
System.out.println("添加成功");
System.out.println("添加失败");
session.close();
在小配置中增加一个节点
&!-- 新增学生信息 --&
&insert id="InsertStuInfo" parameterType="entity.Student" &
insert into Student values(SEQ_NUM.Nextval,#{sname},#{sex},1)
执行之后结果为
后续的删除和修改代码基本上和新增是一致的,只是调用的sql语句不同,所以后续我就不做详细的解释了,只将代码摆出来,详细大家都能够看得明白!!
4.删除学生信息根据id
* 1.4根据SID删除学生信息
public void DeleteStuBySid()throws Exception{
//通过配置文件获取配置信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//构建一个SessionFactory,传入配置文件
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
//获取session
SqlSession session = factory.openSession();
//准备参数
int sid=2;
//调用删除方法
int count = session.delete("Student.DeleteStuBySid", sid);
if(count&0){
System.out.println("删除成功");
System.out.println("删除失败");
session.close();
需要在配置文件中新增的是
&!-- 删除学生信息 --&
&insert id="DeleteStuBySid" parameterType="int"&
delete from Student where sid=#{sid}
&!--或者是
delete from Student where sid=#{_parameter} --&
5.根据SID修改学生信息
* 1.5根据SID修改学生信息
public void UpdateStuBySid()throws Exception{
//通过配置文件获取配置信息
Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
//构建一个SessionFactory,传入配置文件
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
//获取session
SqlSession session = factory.openSession();
//准备参数对象
Student stu=new Student();
stu.setSid(1);
stu.setSname("绿茵");
stu.setSex("女");
//调用删除方法
int count = session.update("Student.UpdateStuBySid", stu);
if(count&0){
System.out.println("修改成功");
System.out.println("修改失败");
session.close();
需要在配置文件中添加的是
&!-- 根据SID修改学生信息 --&
&update id="UpdateStuBySid" parameterType="entity.Student" &
update Student set sname=#{sname},sex=#{sex} where sid=#{sid} --&
update Student
&if test="sname!=null"&
sname=#{sname},
&if test="sex!=null"&
sex=#{sex},
where sid=#{sid}
&以上我们就简单的完成了对Mybatis的增、删、改、查的基本操作了,关于Mybatis的一些高级内容的讲解我会继续在后中为大家持续讲解
阅读(...) 评论()44152人阅读
【Mybatis】(2)
& & &从这篇博文开始我们学习一下Mybatis,希望大家提出宝贵的建议。
什么是Mybatis
& & & &MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)。
& & & MyBatis参考资料官网:
官网对Mybatis的介绍更加具有权威性:&&
MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及抽取结果集。MyBatis 使用简单的 XML 或注解来配置和映射基本体,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
& & & &MyBatis是iBatis的升级版,用法有很多的相&#20284;之处,但是MyBatis进行了重要的改进。例如:
1、Mybatis实现了接口绑定,使用更加方便。
& & & 在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件, 而Mybatis实现了DAO接口与xml映射文件的绑定,自动为我们生成接口的具体实现,使用起来变得更加省事和方便。
2、对象关系映射的改进,效率更高
3、MyBatis采用功能强大的基于OGNL的表达式来消除其他元素。
& & & &对于IBatis与MyBatis的区别就不再多说了,感兴趣的朋友可以研究一下。
MyBatis的框架架构
& & & & & &
& & & & 看到Mybatis的框架图,可以清晰的看到Mybatis的整体核心对象,我更喜欢用自己的图来表达Mybatis的整个的执行流程。如下图所示:
& & & & & & &
原理详解:
& & & & MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。
MyBatis的优缺点
1、简单易学
& & & &mybatis本身就很小且简单。没有任何第三方依赖,最简单安装只要两个jar文件&#43;配置几个sql映射文件易于学习,易于使用,通过文档和源代码,可以比较完全的掌握它的设计思路和实现。
& & & &mybatis不会对应用程序或者数据库的现有设计强加任何影响。 sql写在xml里,便于统一管理和优化。通过sql基本上可以实现我们不使用数据访问框架可以实现的所有功能,或许更多。
3、解除sql与程序代码的耦合
& & & &通过提供DAL层,将业务逻辑和数据访问逻辑分离,使系统的设计更清晰,更易维护,更易单元测试。sql和代码的分离,提高了可维护性。
4、提供映射标签,支持对象与数据库的orm字段关系映射
5、提供对象关系映射标签,支持对象关系组建维护
6、提供xml标签,支持编写动态sql。
1、编写SQL语句时工作量很大,尤其是字段多、关联表多时,更是如此。
2、SQL语句依赖于数据库,导致数据库移植性差,不能更换数据库。
3、框架还是比较简陋,功能尚有缺失,虽然简化了数据绑定代码,但是整个底层数据库查询实际还是要自己写的,工作量也比较大,而且不太容易适应快速数据库修改。
4、二级缓存机制不佳
& & && mybatis的优点同样是mybatis的缺点,正因为mybatis使用简单,数据的可靠性、完整性的瓶颈便更多依赖于程序员对sql的使用水平上了。sql写在xml里,虽然方便了修改、优化和统一浏览,但可读性很低,调试也非常困难,也非常受限。
& & & mybatis没有hibernate那么强大,但是mybatis最大的优点就是简单小巧易于上手,方便浏览修改sql语句。
& & & &下篇博文,我们,马上送到。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1427118次
积分:16071
积分:16071
排名:第434名
原创:169篇
评论:2804条
阅读:46894
阅读:70789
文章:33篇
阅读:396479
文章:25篇
阅读:293997
阅读:37214
文章:11篇
阅读:163413
阅读:244402
(2)(4)(4)(3)(4)(4)(4)(5)(2)(4)(4)(5)(4)(4)(5)(9)(5)(4)(5)(4)(4)(4)(3)(6)(2)(3)(2)(3)(2)(2)(2)(3)(4)(2)(8)(2)(3)(2)(3)(4)(4)(1)(1)(2)(6)(8)(5)(1)(1)

我要回帖

更多关于 easyui修改密码页面 的文章

 

随机推荐