ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

idea框架实现增删改查

2021-09-19 15:35:28  阅读:130  来源: 互联网

标签:String 改查 idea sqlSession student Student 增删 import public


第一步创建一个工程选择maven创建一个新的maven工程

依次创建如图所示的包,并添加TestDao接口与TestDao.xml

同方法创建domain与until包分别创建类student;MyApp。

 resources文件下创建mybatis.xml

 

 创建一个test文件夹并建立相应的包存放testmybatis类其中java文件夹转为test sources root

 testdao接口中写入

package com.hxx.dao;

import com.hxx.domain.Student;

import java.util.List;

//接口 操作test表
public interface TestDao {
    public List<Student> selectStudent();
    public int insertStudent(String student);
    public int updateStudent(String student);
    public int deleteStudent(String student);
}

 testdao.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">
<mapper namespace="com.hxx.dao.TestDao">
    <select id="selectStudent" resultType="com.hxx.domain.Student">
        select id,name,password from test
    </select>
    <insert id="insertStudent">
    insert into test values(#{id},#{name},#{password})
    </insert>
    <update id="updateStudent">
       update test set name=#{name},password=#{password} where id=#{id}
    </update>
    <delete id="deleteStudent">
        delete from test where id=#{id}
    </delete>
</mapper>

domain包下Student类中写入

package com.hxx.domain;

public class Student {
    private int id;
    private String name;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

MyApp类中写入

package com.hxx;

import com.hxx.domain.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 MyApp {
    public static void main(String[] args) throws IOException {
        /**String config="mybatis.xml";
        InputStream in=Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao"+"."+"selectStudent";
        List<Student> studentList=sqlSession.selectList(sqlId);
        for(Student stu:studentList){
            System.out.println("查询结果为:"+stu);
        }
        sqlSession.close();
**/
     /**   String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.insertStudent";
        Student student=new Student();
        student.setId(1);
        student.setName("张三");
        student.setPassword("321");
        int nums=sqlSession.insert(sqlId,student);
        sqlSession.commit();
        System.out.println("插入结果为="+nums);
        sqlSession.close();**/
        /**String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.updateStudent";
        Student student=new Student();
        student.setId(2);
        student.setName("朱某");
        student.setPassword("666");
        int nums=sqlSession.update(sqlId,student);
        sqlSession.commit();
        System.out.println("修改结果="+nums);
        sqlSession.close();**/
       /** String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.deleteStudent";
        Student student=new Student();
        student.setId(4);
        int nums=sqlSession.delete(sqlId,student);
        sqlSession.commit();
        System.out.println("删除结果="+nums);
        sqlSession.close();
**/
    }
}

mybatis.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="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/student?characterEncoding=utf8"/>
                <property name="username" value="root"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/hxx/dao/TestDao.xml"/>
    </mappers>
</configuration>
TestMybatis测试类中写入
package com.hxx;

import com.hxx.domain.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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMybatis {
    @Test
    public void testselect() throws IOException {
        String config="mybatis.xml";
        InputStream in=Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao"+"."+"selectStudent";
        List<Student> studentList=sqlSession.selectList(sqlId);
        for(Student stu:studentList){
            System.out.println("查询结果为:"+stu);
        }
        sqlSession.close();
    }
    @Test
    public void testinsert() throws IOException {
        String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.insertStudent";
        Student student=new Student();
        student.setId(2);
        student.setName("猪猪侠");
        student.setPassword("777");
        int nums=sqlSession.insert(sqlId,student);
        sqlSession.commit();
        System.out.println("插入结果为="+nums);
        sqlSession.close();
    }
    @Test
    public void testupdate() throws IOException {
        String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.updateStudent";
        Student student=new Student();
        student.setId(2);
        student.setName("奥特曼");
        student.setPassword("666");
        int nums=sqlSession.update(sqlId,student);
        sqlSession.commit();
        System.out.println("修改结果="+nums);
        sqlSession.close();
    }
    @Test
    public void testdelete() throws IOException {
        String config="mybatis.xml";
        InputStream in= Resources.getResourceAsStream(config);
        SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
        SqlSessionFactory factory=builder.build(in);
        SqlSession sqlSession=factory.openSession();
        String sqlId="com.hxx.dao.TestDao.deleteStudent";
        Student student=new Student();
        student.setId(2);
        int nums=sqlSession.delete(sqlId,student);
        sqlSession.commit();
        System.out.println("删除结果="+nums);
        sqlSession.close();

    }
}

最后于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>1</groupId>
  <artifactId>ch01-hello-mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.10</version>
    </dependency>
  </dependencies>

  <build>
    <resources>
      <resource>
        <directory>src/main/java</directory><!--所在的目录-->
        <includes><!--包括目录下的.properties..xml 文件都会扫描到-->
        <include>**/*.properties</include>
        <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

  </build>
</project>

至此完成整个程序!

标签:String,改查,idea,sqlSession,student,Student,增删,import,public
来源: https://blog.csdn.net/chentianxiao_/article/details/120380624

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有