五邑web程序设计03.综合理论课mybatis第一章_第1页
五邑web程序设计03.综合理论课mybatis第一章_第2页
五邑web程序设计03.综合理论课mybatis第一章_第3页
五邑web程序设计03.综合理论课mybatis第一章_第4页
五邑web程序设计03.综合理论课mybatis第一章_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JavaEE企业级框架-SSM1.JavaEE框架概Struts2MVCJavaEE企业级框架-SSM1.JavaEE框架概Struts2MVCweb 封装Hibernate底层封装层Spring IOC AOPspring MyBatis 层SpringSpring IOC AOPspring MyBatis 层SpringMVCwebSpring框架提供的构建Web应用程序的MVCSpringMVCSpring 1.2.Mybatis31.2.Mybatis3 =2.1.MybatisMyBatisapache的一个开源项目iBatis2010年这个项目由apachefoundation

2、。SQL2.1.MybatisMyBatisapache的一个开源项目iBatis2010年这个项目由apachefoundation 。SQLemen(s2.1.2.mybatis官网jar :MybatisSpringjar MybatisSpringjar 2.2.mybatis2.3.mybatismybatis 2.3.mybatismybatis Mybatis配置文件主要用于数据库连接和mybatisMybatissourceMybatis配置文件主要用于数据库连接和mybatisMybatissourcefoldersrc mybatis的DTD头文件(在哪里找:mybatis

3、 则XML ?xml=1.0encoding=UTF-8!DOCTYPEPUBLIC-/DTDConfig !-和spring整合后environments配置-environmentsenvironment ionManager type=JDBC!- 数据库连接池dataSourcepropertyname=driver.jdbc.Driver property:/localhost:3306/mybatis?characterEncoding=utf-8 propertyname=usernamevalue=rootpropertyname=passwordvalue=12345name

4、space id parameterType namespace id parameterType mybatismapper?xml=1.0encoding=UTF-8!DOCTYPEPUBLIC-/DTDMapper mapperselect id=selecctById parameterType= resultType=com.shop.select * from tb_user where id=#idStringconfig=mybatis-InputStreamStringconfig=mybatis-InputStream is=创建 Factorysf=创建 对象,该对象的作

5、用是调用 mapper 文件进行数据操作(一定要先引=使用sql.selectOne(test.selecctById,关闭 对象 2.4.mybatis1)根据用户 id 查询,返回一个用2.4.mybatis1)根据用户 id 查询,返回一个用2)根模糊查询,返回用户列2.1)selectid=selectUserListByNameparameterType=Stringselect *from tb_userwherename public void fun1() throws IOException Stringconfig=mybatis-config.xml;InputStrea

6、minputStream =Factory sf = new SqlSes= sf.openSesUser user = sesasUserById,12); mapperselect id=selectUserById parameterType= resultType=com.shop.select * from tb_user where id= #id2.2)selectid=selectUserListByName2parameterType=2.2)selectid=selectUserListByName2parameterType=String select*fromtb_us

7、erwherenamelikepublic void fun2() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream=Factory sf = new SqlSes= sf.openSesListuserList= for (User user : userList) 3)查询所有用户,select id=selectUser resultType3)查询所有用户,select id=selectUser resultType=com.shop.select * from tb_userpubli

8、c void fun4() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream=public void fun3() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream =Factory sf = new SqlSes= sf.openSesListuserList=.selectList(selectUserListByName2, for (User user : userList) 1)基publi

9、c void fun5() throws IOException Stringconfig1)基public void fun5() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream=Factory sf = new SqlSes= sf.openSesUseruser=newUser(); .insert(insertUser,user); insert id=insertUser parameterType=com.shop.O tb_user (name,age) values (Facto

10、ry sf = new SqlSes= sf.openSesListuserList=for (User user : userList) 2)事务提 2)事务提public void fun5() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream =Factory sf = new SqlSes= sf.openSesUseruser=newUser(); .insert(insertUser,mybatis默认是不自动提交事id public void fun6() throws IOExce

11、ption Stringconfig=id public void fun6() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream =insert id=insertUser2 parameterType=com.shop.UseruseGeneratedKeys=true !- SELECT LAST_INSERT_ID()-Otb_user (name,age)values insertid=insertUser2 SELECT LAST_INSERT_ID()Otb_user (name,a

12、ge)values 4)uuid 主键public void fun7() throws IOException String4)uuid 主键public void fun7() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream =Factory sf = new SqlSes= sf.openSesinsert id=insertUser3 parameterType=com.shop.UseruseGeneratedKeys=true SELECT UUID()O tb_user (name

13、,age,uuid)values Factory sf = new SqlSes= sf.openSesUseruser=newUser(); .insert(insertUser2,user); public void fun8() throws IOException Stringpublic void fun8() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream=Factory sf = new SqlSes = sf.openSesUser user=newUser(); User us

14、er = new User(); .insert(insertUser3,user); 3.1.myabtispublic void 3.1.myabtispublic void fun9() throws IOException Stringconfig=mybatis-config.xml;InputStreaminputStream=Factory sf = new SqlSes= sf.openSes.delete(test.deleteUser,36); ConfigStreamReader Factory 创建 ConfigStreamReader Factory 创建Factor

15、y mybatis Factory JDBCconnection3.5.mybatisUtils Factory3.5.mybatisUtils Factory使用进4.2.mapper 4.2.1.mapper sql使用进4.2.mapper 4.2.1.mapper sqlid4.2.2.mapper接口(重点文件编?xml=1.0encoding=UTF-8!DOCTYPEPUBLIC-/DTD Mappermapperselectid=selectUserByIdSELECT *from tb_userWHEREid= selectid=selectUser2)编写Mapper接2)

16、编写Mapper接packageimporterfaceUserMapperpublicUseregerSELECT*from insertid=insertUserOupdateid=updateUserUPDATEtb_userSET,age=#age,gender=#genderwhereid=delete id=deleteUser parameterType=delete from tb_user where id = #id3)Mapper 4)测3)Mapper 4)测publicvoidfun4()throwsIOExceptionmybatis配置文StringconfigX

17、ml=mybatis-InputStreaminputStream=Factory= new= sqlSesUserMapper userMapper = sesUser user = userMapper.selectUserById(35);配置文myabtis 注意:一定要注配置文myabtis 注意:一定要注意节点顺序,如果顺序错了,IDE 会错Configuration ,mybatis 5.2.1. typeAliasalias=usertypeAliasalias=producttype=cn.qn.entitytypeAliasalias=customertype=typeAliasalias=ordertype=cn.qn.entity如果是有很多个实体对象如果是有很多个实体对象,意味着仍然需要写很多个typeA

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论