MyBatis学习总结
来源:互联网 发布:mac给iphone安装软件 编辑:程序博客网 时间:2024/06/06 07:13
1.mybatis的结构如下图所示:
- src : 存放Java代码
- config : 存放相关的配置文件,SqLMapConfig.xml作用相当于Hibernate中的Hibernate.cfg.xml,还有User.xml文件
2.SqLMapConfig.xml配置
<environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理 --> <transactionManager type="JDBC" /> <!-- 数据库连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="User.xml" /> </mappers>
3.User.xml配置
1.
<!-- namespace:命名空间,做sql隔离,后面在方法调用操作数据库时会用 --><mapper namespace="test"></mapper>
2.增删改查 - 增标签:`<insert></insert>` - 删标签:`<delete></delete>` - 改标签:`<update></update>` - 查标签:`<select></select>`
使用参考下:
<!-- id:sql语句唯一标识 parameterType:指定传入参数类型 resultType:返回结果集类型--> <select id="findUserById" parameterType="java.lang.Integer" resultType="cn.itheima.pojo.User"><!--此处写SQL语句--> select * from user where id=#{id} </select>
- 查询语句:select * from 表名
- 条件查询语句:select * from 表名 where (条件) = ?
- 增加语句:insert into 表名(属性值,不写默认为全部属性) value (属性值,注意传入的类型要与数据库中的类型一致)
- 删除语句:delete from 表名 where (条件)
- 修改语句:update 表名 set (属性值) = (新值) where (条件)
4.Dao方法:
String resource = "SqlMapConfig.xml"; //通过流将核心配置文件读取进来 InputStream inputStream = Resources.getResourceAsStream(resource); //通过核心配置文件输入流来创建会话工厂 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream); //通过工厂创建会话 SqlSession openSession = factory.openSession(); //查询,返回值有三种类型1、openSession.selectOne, 2、openSession.selectList 3、openSession.selectMap //增和改的时候需要先创建对象,往对象赋值,在注入SQL语句,需要提交事务
User user = new User(); user.setId(28); user.setUsername("王麻子"); openSession.update("test.updateUserById", user); // newspaces +"."+id 与配置文件中的属性一致
//删除 openSession.delete("test.delUserById", 29); //提交 openSession.commit();
阅读全文
0 0
- Mybatis学习总结
- mybatis学习总结
- Mybatis学习总结
- mybatis学习总结
- Mybatis学习总结
- MyBatis学习总结一
- MyBatis学习总结(二)
- MyBatis的学习总结
- MyBatis学习总结
- MyBatis学习总结
- Mybatis学习与总结
- Mybatis学习总结
- Mybatis学习总结一
- Mybatis学习总结二
- Mybatis学习总结
- MyBatis学习总结
- Mybatis学习总结
- Mybatis学习总结二
- 安装ssh--csdn 真坑,我好不容易整理的一大堆问题,不小心关掉浏览器全没了,能不能给个提醒
- redis命令
- 如鹏网学习笔记(十)DOM
- java基础学习总结——this关键字
- SSH-远程登录协议
- MyBatis学习总结
- Qt5.9.1_MSVC2017版本调试环境安装
- python数据结构
- java基础学习总结——super关键字
- android 屏幕属性
- js刷新一次
- 设计模式—职责连模式
- 分糖果系列一(DP+暴力)
- 【Noip2013】积木游戏