SSMybatis整合 --详细解读Mybatis对oracle数据库进行增删改查(一)

来源:互联网 发布:恺英网络造假 编辑:程序博客网 时间:2024/06/06 00:08

Mybatis是现在主流的持久化层框架,与hibernate不同的是,它鼓励程序员使用原声SQL语句对数据库进行操作.因此提供了非常灵活的功能.特别是当数据库同时访问数过多,需要进行优化时,使用sql语句进行优化远比使用Hibernate的查询语句简单得多. 
Mybatis也有它的缺点.因为它是使用原生的SQL语句,所以他的数据库兼容性不高.但是这并不妨碍它的作用,接下来,就让我们简单使用一下Mybatis. 
我使用的是Eclipse,数据库为Oracle 创建一个Java项目 
首先,像hibernate.cfg.xml一样,我们需要创建一个mybatisConfig.xml(名字任意取),在这个配置文件中执行对数据库的连接

    <configuration>    <!--2-->    <typeAliases>        <!-- 别名 因为实体类的名称几乎总是冗长的,我们有必要为它去一个别名-->        <typeAlias type="com.jacx.entity.Emp" alias="emp" />    </typeAliases>    <!--1-->    <environments default="oracle">        <environment id="oracle">            <transactionManager type="JDBC"></transactionManager>            <!--使用线程池-->            <dataSource type="POOLED">                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />                <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />                <property name="username" value="scott" />                <property name="password" value="tiger" />            </dataSource>        </environment>    </environments>    <!-- 配置加载映射文件 -->    <!--3-->    <mappers>        <mapper resource="com/jacx/config/empMapper.xml" />    </mappers></configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

对数据库进行连接后,我们需要创建一个实体类.注意在这个时候我们并没有将实体类和数据库表进行关系映射,想想我们怎么通过实体字段找到对应的数据库字段 
当没有写进行关系映射时,如果数据库名和实体变量名一致,他依然会对应

    private Integer empNo;    private String eName;    private String job;    private String mgr;    private String sal;    private String comm;    private String deptNo;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

然后生成它的get set方法即可

接下来我们创建一个empMapper.xml,写SQL语句进行增删改查的操作,我会在代码备注中一一解释他们的作用

<!--namespace不能为空,它指向你的mapper的路径,每个实体(表)都要有一个mapper.xml--><mapper namespace="com.jacx.config.empMapper">    <!--id=相当于方法名,在客户端中进行调用,resultType=执行本操作返回的类型,比如我执行的是查询操作,他就返回emp;注意,这里填emp是因为我在mybatisConfig.xml中写的别名是emp.resultMap,emp实体映射的数据库字段-->    <select id="queryAll" resultType="emp" resultMap="empResultMap">        SELECT * FROM emp    </select>    <select id="queryById" resultType="Emp" parameterType="int" resultMap="empResultMap">    Select * FROM emp WHERE empNo = #{id}    </select>    <delete id="deleteById" parameterType="int">    DELETE FROM emp WHERE empNo = #{id}    </delete>    <update id="updateEmp" parameterType="Emp">    UPDATE emp     <set>    <if test="eName!=null">        ENAME = #{eName},    </if>    <if test="job!=null">        JOB = #{job},    </if>    <if test="mgr!=null">        MGR = #{mgr},    </if>    <if test="sal!=null">        SAL = #{sal},    </if>    <if test="comm!=null">        COMM = #{comm},    </if>    <if test="deptNo!=null">        DEPTNO = #{deptNo}    </if>    </set>    WHERE empNo = #{empNo}    </update>    <!--实体通过resultMap知道自己的变量对应哪个字段.column为数据库中的列明,property为实体类中的变量名,id是增删改中resultMap用到的-->    <resultMap type="Emp" id="empResultMap">        <id column="EMPNO" property="empNo" />        <result column="ENAME" property="eName" />        <result column="JOB" property="job" />        <result column="MGR" property="mgr" />        <result column="SAL" property="sal" />        <result column="COMM" property="comm" />        <result column="DEPTNO" property="deptNo" />    </resultMap>    </mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
然后把它加到mybatisConfig.xml中接下来就可以在客户端里进行操作了(这是main方法中的代码)
 Reader reader = null;     SqlSessionFactoryBuilder sqlSessionFactoryBuilder = null;     SqlSessionFactory sqlSessionFactory = null;     SqlSession sqlSession = null;    try {            reader = Resources.getResourceAsReader("mybatisConfig.xml");            sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();            sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);            sqlSession = sqlSessionFactory.openSession();            //查            //引号中的代码为mapmer.xml的路径加上对应方法的id            List<Emp> empList = sqlSession.selectList("com.jacx.config.empMapper.queryAll");            for (Emp e : empList) {                System.out.println(e.getDeptNo() + "\t" + e.geteName() + "\t" + e.getJob() + "\t" + e.getMgr() + "\t"                    + e.getSal() + "\t" + e.getEmpNo());            }            //改            Emp emp = new Emp();            emp.seteName("jac");            emp.setEmpNo(7499);            sqlSession.update("com.jacx.config.empMapper.updateEmp",emp);            //删            sqlSession.delete("com.jacx.config.empMapper.deleteById", 7521);            //增  在增加功能中,我们需要为数据库emp表添加一个序列,因为oracle不允许自动增长            Emp emp = new Emp();            emp.seteName("123");            sqlSession.insert("com.jacx.config.empMapper.addEmp")            sqlSession.commit();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }
阅读全文
0 0
原创粉丝点击