MyBatis基础知识
来源:互联网 发布:正规网络兼职打字员 编辑:程序博客网 时间:2024/05/22 04:28
MyBatis获取SqlSession
首先获取SqlSessionFactory:
Reader reader=Resources.getResourceAsReader(“Configuration.xml”);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(reader);
获取SqlSession:
sf.openSession();
deptDTO:属性 deptno、dname、loc;
DeptMapper:增删改查方法
定义命名空间 :
定义结果集:
<resultMap id="RM.Dept" type="com.test.dto.DeptDTO"> <result property="deptno" column="deptno"/> <result property="dname" column="dname"/> <result property="loc" column="loc"/></resultMap>
插入insert:
–插入需要一个参数DTO,参数类型为:com.test.dto.DeptDTO
<insert id="Dept.insert" parameterType="com.test.dto.DeptDTO"> <![CDATA[ INSERT INTO dept ( deptno, dname, loc ) VALUES ( #{deptno} , #{dname} , #{loc} ) ]]> </insert>
DAO-INSERT: public void insert(Object obj){ SqlSession session=SqlSessionFactoryUtil.getSqlSession(); session.insert(namespace+"insert",obj); session.commit(); }
删除delete:
–删除时根据id删除 参数类型为int
<delete id="Dept.delete" parameterType="int"> <![CDATA[ DELETE FROM dept WHERE deptno = #{deptno} ]]> </delete>
public void delete(int id){ SqlSession session=SqlSessionFactoryUtil.getSqlSession(); session.delete(namespace+"delete", id); session.commit(); }
修改update:
<update id="Dept.update" > <![CDATA[ UPDATE dept SET dname=#{dname}, loc=#{loc} WHERE deptno = #{deptno} ]]> </update>
public void update(Object obj){ SqlSession session=SqlSessionFactoryUtil.getSqlSession(); session.update(namespace+"update",obj); session.commit(); }
查询:
<select id="Dept.getById" resultMap="RM.Dept"> SELECT <include refid="Dept.columns" /> <![CDATA[ FROM dept WHERE deptno = #{deptno} ]]> </select>
public Object query(int id){ SqlSession session=SqlSessionFactoryUtil.getSqlSession(); Object obj=session.selectOne(namespace+"getById", id); return obj; }
Configuration.xml文件:根据数据库不同,配置不同的参数。
<configuration> <environments default="myexample"> <environment id="myexample"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/emp" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/test/mapper/DeptMapper.xml"/> </mappers></configuration>
TIPS:
- 可以定义一个BaseDAO,然后需要dao时继承该dao就可以了。
- 定义SqlSessionFactoryUtil类,在类中获取sqlsession,直接调用该类即可。
SqlSessionFactoryUtil:public class SqlSessionFactoryUtil {// 定义一个私有的构造方法 单例模式 private SqlSessionFactoryUtil(){} private static SqlSessionFactory sf;// 静态块 初始化Configuration.xml static { try { Reader reader=Resources.getResourceAsReader("Configuration.xml"); sf=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } }// 如果静态语句块初始化失败,尝试再初始化一次 public static SqlSessionFactory getSqlSessionFactory(){ if(sf==null){ sf=initSqlSessionFactory(); } return sf; } public static SqlSessionFactory initSqlSessionFactory(){ try { Reader reader=Resources.getResourceAsReader("Configuration.xml"); sf=new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } return sf; } public static SqlSession getSqlSession(){ return sf.openSession(); }}
我的心愿是:
好好学习
天天向上
世界和平
!
0 0
- mybatis基础知识
- Mybatis基础知识
- MyBatis基础知识
- Mybatis基础知识
- MyBatis基础知识
- mybatis基础知识
- mybatis基础知识-2.mybatis框架
- Mybatis基础知识(1)
- Mybatis基础知识--番外篇
- Mybatis基础知识(2)
- Mybatis基础知识(3)
- mybatis的基础知识
- 总结mybatis基础知识
- 1、mybatis基础知识
- MyBatis01 MyBatis基础知识
- Mybatis 学习---基础知识
- mybatis的基础知识 (一)
- Mybatis基础知识--框架原理篇
- Radiogroup+radioButton+fragment
- Python项目模拟登录学校正方教务系统抓取课程表。
- win10.64位搭建Php开发环境(PHP7.0.11+Apache2.4.23+Mysql-5.7.15 )安装配置图文教程
- Android开发之手把手教你写ButterKnife框架(二)
- 欧洲净水霸主BARRIER正式登陆中国 创新中国人饮水方式
- MyBatis基础知识
- 习题7--更多打印
- LwIP的操作系统模拟层移植
- XML的解析02
- Unity3D - 动作动画忽略timeScale
- 顺序表应用8:最大子段和之动态规划法
- SSM + Shiro 整合 (3)- 添加 Spring MVC 成为 Web 项目
- ReentrantLock使用场景和实例
- 错误:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transfo