码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis01

时间:2020-04-30 13:27:45      阅读:46      评论:0      收藏:0      [点我收藏+]

标签:ssi   操作   where   图片   pen   apache   tran   sele   utf-8   

技术图片

 

entity:实体类,即是数据表的映射

package entity;

public class Student {
    private int StuNo;
    private String StuName;
    private int StuAge;
    private String graName;
    
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(int stuNo, String stuName, int stuAge, String stuSex) {
        super();
        StuNo = stuNo;
        StuName = stuName;
        StuAge = stuAge;
        graName = stuSex;
    }
    public int getStuNo() {
        return StuNo;
    }
    public void setStuNo(int stuNo) {
        StuNo = stuNo;
    }
    public String getStuName() {
        return StuName;
    }
    public void setStuName(String stuName) {
        StuName = stuName;
    }
    public int getStuAge() {
        return StuAge;
    }
    public void setStuAge(int stuAge) {
        StuAge = stuAge;
    }
    public String getStuGraName() {
        return graName;
    }
    public void setStuGraName(String stuGraName) {
        graName = stuGraName;
    }
    @Override
    public String toString() {
        return this.StuNo+"-"+this.StuName+"-"+this.StuAge+"-"+this.graName;
        
    }
}

 mapper:即是dao层,对数据的操作

<?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="Mapper.StudentMapper">

    <select id="selectStudentByStuNo"
        resultType="entity.Student" parameterType="int">
        select * from student
        where StuNo = #{StuNo}
    </select>

    <insert id="AddStudent" parameterType="entity.Student">
        INSERT INTO
        `test`.`student` (`StuNo`, `StuName`, `StuAge`, `graName`)
        VALUES
        (#{StuNo},#{StuName},#{StuAge},#{graName});
    </insert>

    <delete id="DeleteStudentByStuNO" parameterType="int">
        delete from
        student where StuNo=#{StuNo}
    </delete>

    <update id="UpdateStudentByStuNo"
        parameterType="entity.Student">
        update student set StuName=#{StuName}, StuAge=#{StuAge},
        StugraName=
        #{StugraName} where StuNo=#{StuNo}
    </update>

    <select id="selectAll" resultType="entity.Student">
        select * from student
    </select>
</mapper>

config.xml:mybatis的配置文件

<?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.cj.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/>
 <property name="username" value="root"/>
 <property name="password" value=""/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 <mapper resource="Mapper/StudentMapper.xml"/>
 </mappers>
</configuration>

test:测试

package mybatis01;

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

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

import entity.Student;


public class TestMybatis {
     
    public static SqlSession MybatisUtil() throws IOException {
        //引入配置文件
        Reader reader = Resources.getResourceAsReader("config.xml");
        //创建一个sqlSession工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //得到工厂生产的sqlSession
        SqlSession sqlSession = sessionFactory .openSession();
        return sqlSession;
    }
    
    
    public static void QueryByNo() throws IOException {
        SqlSession sqlSession = MybatisUtil();
        //Mapper.StudentMapper是mapper所在路径,selectStudentByStuNo是mapper中方法的id
        String statement  = "Mapper.StudentMapper.selectStudentByStuNo";
        Student student = sqlSession.selectOne(statement, 1);//查询stuno=1的人
        System.out.println(student.toString());
    }
    
    public static List<Student> QueryAll() throws IOException {
        SqlSession sqlSession = MybatisUtil();
        String statement  = "Mapper.StudentMapper.selectAll";
        List<Student> list = sqlSession.selectList(statement);
        return list;
    }
    
    public static void Add() throws IOException {
        SqlSession sqlSession = MybatisUtil();
        String statement  = "Mapper.StudentMapper.AddStudent";
        int i = 0;
        Student student = new Student();
        student.setStuNo(1);
        student.setStuName("zs");
        student.setStuAge(18);
        student.setStuGraName("PU");
        i = sqlSession.insert(statement,student );
        
    }
    public static void close(SqlSession sqlSession) {
        sqlSession.close();
    }
    public static void main(String [] grgs) {
        
    }
    
    
}

mybatis01

标签:ssi   操作   where   图片   pen   apache   tran   sele   utf-8   

原文地址:https://www.cnblogs.com/adimio/p/12807979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!