mybatis学习1--基础的数据操作

 
maven依赖 : https://mvnrepository.com/
 
1.配置mybatis-config.xml文件,也就是要连接的数据库。
 
<? 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 >
    <!-- 别名 -->
    < typeAliases >
        < package name= "com.test2" />
    </ typeAliases >
    <!-- 数据库环境 -->
    < 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/test?characterEncoding=UTF-8" />
                < property name= "username" value= "root" />
                < property name= "password" value= "root" />
            </ dataSource >
        </ environment >
    </ environments >
    <!-- 映射文件 也就是数据库要操作那些个表-->
    < mappers >
        < mapper resource= "./student.xml" />
    </ mappers >
</ configuration >
 
2.配置要操作的数据表文件 student.xml
 
<? xml version= "1.0" encoding= "UTF-8" ?>
<! DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 进行操作当前表 -->
< mapper namespace= "test2" >
      <!-- parameterType:参数类型 resultType:返回的结果类型 -->
      < select id= "listStudent" resultType= "Student" >
          select * from student
      </ select >
      < insert id= "addStudent" parameterType= "Student" >
          insert into student ( sid, name, sex) values (#{ sid},#{name},#{sex})
      </ insert >
      < delete id= "deleteStudent" parameterType= "_int" >
          delete from student where sid = #{ sid}
      </ delete >
      < select id= "getStudent" parameterType= "_int"
           resultType= "Student" >
          select * from student where sid = #{ sid}
      </ select >
      < update id= "updateStudent" parameterType= "Student" >
      <!-- 同时修改两个属性 -->
          update student set name=#{name} ,sex = #{sex} where sid = #{ sid}
      </ update >
</ mapper >
 
3.进行操作数据库
 
public static void main(String args[]) {
           // 根据 mybatis -config.xml 配置的信息得到 sqlSessionFactory
          String resource = "mybatis-config.xml";
          InputStream inputStream = null;
           try {
               inputStream = Resources. getResourceAsStream( resource);
              SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build( inputStream);
               // 通过session进行数据库的操作
               // 这里使用true表示的是要提交事务,如果不添加参数,那么默认是不提交事务的
               // 也就是说不真正的修改数据库
//            SqlSession session = sqlSessionFactory.openSession(true);
//            现在测试阶段,不真正的修改数据库
              SqlSession session = sqlSessionFactory.openSession();
              Student student1 = new Student();
               student1.setSid(18);
               student1.setName( "大美妞");
               student1.setSex( "妞");
//            进行增删改查的时候,在第一个参数中填入的是你在 xml 中配置的查询语句的名称
              //增
               session.insert( "addStudent", student1);
              //删
               session.delete( "deleteStudent", 0);
              //查
              Student selectOne = session.selectOne( "getStudent", 18);
              System. out.println( "选择一个==" + selectOne.toString());
              //更新
              Student student2 = new Student();
               student2.setSid(5);
               student2.setName( "大妞");
               student2.setSex( "很不错");
               session.update( "updateStudent", student2);
              List<Student> listStudent = session.selectList( "listStudent");
               for (Student student : listStudent) {
                   System. out.println( student.toString());
              }
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值