当前位置:Gxlcms > 数据库问题 > springboot+oracle+mybatis实现crud

springboot+oracle+mybatis实现crud

时间:2021-07-01 10:21:17 帮助过:29人阅读

com.ximen.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data//生成get和set方法 @AllArgsConstructor//生成有参构造 @NoArgsConstructor//生成无参构造 public class user { private int StuId; private int Age; private int ClassId; private String StuName; private String Gender; //private String JoinDate; }

3.userMapper代码

package com.ximen.mappers;

import com.ximen.entity.user;

import java.util.List;

public interface userMapper {
    public int save(user user);
    public int delete(int StuId);
    public List<user> findAll();
    public user findById(int StuId);
    public int update(user user);
}

4.userMapper.xml

<?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">
    <!--namespace:实现接口的全类名-->
    <mapper namespace="com.ximen.mappers.userMapper">
    <!--    增: id:接口方法名  parameterType:传入参数的类型-->
    <insert id="save" parameterType="com.ximen.entity.user">
        insert into student(StuId, Age,StuName,Gender,ClassId) VALUES (#{StuId},#{Age},#{StuName},#{Gender},#{ClassId})
    </insert>

    <!--    删-->
    <delete id="delete" parameterType="int">
        delete from student where StuId=#{StuId}
    </delete>

    <!--    查找所有用户: resultType:返回的结果类型-->
    <select id="findAll" resultType="com.ximen.entity.user">
        select * from student
    </select>

    <!--    通过id查找用户-->
    <select id="findById" parameterType="int" resultType="com.ximen.entity.user">
        select * from student where StuId=#{StuId}
    </select>

    <!--    修改-->
    <update id="update" parameterType="com.ximen.entity.user">
        update user set name=#{name},password=#{password} where id=#{StuId}
    </update>
    </mapper>

5.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>
    <environments default="db1">
        <environment id="db1">
            <!--            配置JDBC事务-->
            <transactionManager type="JDBC"/>
            <!--            POOLED配置数据源连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.OracleDriver"/>
                <property name="url" value="jdbc:oracle:thin:@192.168.7.136:1521/orcl"/>
                <property name="username" value="banhan"/>
                <property name="password" value="pass"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/ximen/mappers/userMapper.xml"/>
    </mappers>
</configuration>

6.Test类

import com.ximen.entity.user;
import com.ximen.mappers.userMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //通过类加载器加载配置文件
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        userMapper mapper = sqlSession.getMapper(userMapper.class);
        //获取当前系统时间
        Date date = new Date();
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("当前时间为: " + ft.format(dNow));
        //        查询所有用户  注:有数据变动的才用commit提交事务,像查询的话是没有数据变动的,所以不用commit提交事务
      //  List<user> users = mapper.findAll();
      //  for(user user:users){
      //      System.out.println(user);
     //   }
     //   sqlSession.close();
        ////        User user=new User(2,"giao2","123");
//        int save = mapper.save(user);
//        sqlSession.commit();//提交事务,有数据变动必须提交事务
//        System.out.println(save);
//        sqlSession.close();//关闭sqlSession避免资源浪费

        //
        int delete = mapper.delete(3);
        sqlSession.commit();
        System.out.println(delete);
       // sqlSession.close();

        //通过id查找用户
          user user = mapper.findById(2);
          int mm=user.getAge();
          System.out.println("查询姓名为"+user.getStuName());
          System.out.println("*******查*******");
          System.out.println("查询年龄为"+mm);
          System.out.println("*******增*******");
          Scanner scr  = new Scanner(System.in);
          int  sc =scr.nextInt();
          if (sc ==1){
              user useradd=new user(3,99,9526,"bruce","男");
              int save = mapper.save(useradd);
              sqlSession.commit();//提交事务,有数据变动必须提交事务
              System.out.println(save+"条添加成功");
          }else{
              System.out.println("未选择追加");
          }
          sqlSession.close();

        //修改
//        User user = new User(2, "giao2", "321");
//        int update = mapper.update(user);
//        sqlSession.commit();
//        System.out.println(update);
//        sqlSession.close();
    }
}

7.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mybatisTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

 

springboot+oracle+mybatis实现crud

标签:class   nfa   sim   ssi   没有   mode   users   drive   sch   

人气教程排行