Mybatis 01

1、在实际的开发中json处理包括以下几种方式:
   第一种:使用字符串拼接方式(这种方式效率最高,容错性最差。编码太困难。)
   第二种:使用各种插件:
      jackson
      jsonlib
      fastjson
      gson
      ....
2、json解析包括两个方向:
   一个是将java对象转换成json格式的字符串。
   一个是将json格式的字符串逆转成java对象。
   
   java对象 <-----> json字符串

3、使用jackson插件将java对象转换成json字符串两行代码搞定:
   ObjectMapper om = new ObjectMapper();
   String json = om.writeValueAsString(java对象);
public class Jackson01 {
    public static void main(String[] args) throws IOException {
        //对象转json
        User user = new User();
        user.setName("lihua");
        user.setAge(30);

        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(user);
        System.out.println(s);
// 运行结果:
//        {"name":"lihua","age":30}
    }
}
4、JDBC开发的缺点?

   * 缺点一:重复代码太多,这样会让开发效率降低。(比较繁琐,有一些代码是“没有必要的重复”)
      while(rs.next()){
         ...
         // 反复的调用同一个方法:String str = rs.getString("只有这里不同")
         String id = rs.getString("id");
         String name = rs.getString("name");
         String birth = rs.getString("birth");
         
         Student student = new Student();
         // 反复的调用同一个方法:setXxx(xx)
         student.setId(id);
         student.setName(name);
         student.setBirth(birth);
         ...
      }
      
      以上的代码中反复的从结果集中取数据,反复的调用对象的set方法给对象的属性赋值,
      这个过程完全可以使用反射机制替代,mybatis框架就是别人提前写好的java代码,
      这个mybatis框架封装了JDBC代码,mybatis框架当中使用了反射机制,帮助我们自动
      创建java对象,自动给java对象的属性赋值,以上的代码在mybatis中不需要编写了。
      
   * 缺点二:JDBC开发当中sql语句是编写在java程序当中的,sql语句不支持配置。sql语句
   可能后期需要调优,sql语句被修改的概率还是很高的。在java程序中编写sql语句,后期修改
   sql语句的时候,需要重新修改java源代码,源代码的修改会导致重新编译/重新部署等操作。
   并且修改java源代码已经违背了开闭原则:OCP。
      互联网分布式架构方面的项目,并发量很大,系统需要不断的优化,各方面优化,其中有一条
      非常重要的优化就是sql优化。
package mybatis02;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Jdbc {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Student> list = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user = "root";
            String password = "root";
            String url = "jdbc:mysql://localhost:3306/catering_management";
            conn = DriverManager.getConnection(url,user,password);

            String sql = "select * from student";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                String name = rs.getString("name");
                int age = rs.getInt("age");
                Student student = new Student(name,age);
                list.add(student);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        for (Student student : list) {
            System.out.println(student);
        }
    }
}

编写第一个Mybatis 程序:
1,引入jar包

2,编写配置文件

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/catering_management" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="SqlMapper.xml" />
    </mappers>
</configuration>

 

<?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">
<mapper namespace="hhhhh">
    <select id="getAll" resultType="mybatis02.Student">
        select * from student
    </select>
</mapper>

3,编写java程序

package mybatis03;

import mybatis02.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Ch01 {
    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            sqlSession = sqlSessionFactory.openSession();
            List<Student> stuList = sqlSession.selectList("getAll");
            for (Student student : stuList) {
                System.out.println(student);
            }
            sqlSession.commit();
        } catch (IOException e) {
            //出现异常回滚
            if(sqlSession != null){
                sqlSession.rollback();
            }

            throw new RuntimeException(e);
        }finally {
            if(sqlSession != null){
                sqlSession.close();
            }
        }
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值