栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis使用教程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis使用教程

maven+thymeleaf+Mybatis+mvc

(1)创建数据库student

 (搭建配置文件)目录结构

pom.xml中


        
            junit
            junit
            4.11
            test
        
        
            org.springframework
            spring-webmvc
            5.3.12
        

        
            javax.servlet
            servlet-api
            2.5
        

        
            javax.servlet.jsp
            jsp-api
            2.2
        

        
            javax.servlet
            jstl
            1.2
        

        
            org.thymeleaf
            thymeleaf
            3.0.9.RELEASE
        

        
        
            org.thymeleaf
            thymeleaf-spring4
            3.0.9.RELEASE
        

        
            mysql
            mysql-connector-java
            5.1.10
        

        
            org.mybatis
            mybatis
            3.4.6
        


        
            org.projectlombok
            lombok
            1.18.12
            provided
        

        
            com.alibaba
            druid
            1.2.8
        

        
            com.alibaba
            fastjson
            1.2.41
            compile
        
    

(2) 搭建mybatis-config.xml配置文件用来映射数据库





    
    
    
    
        
            
            
            
                
                
                
                
                
            
        
    

    
        
        
    

还需要创建一个实体映射类

package com.shi;

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 MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取SqlSession连接
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
}

(3)发现上面导入是

发现注入文件没有,创建一下db.properties

db.url=jdbc:mysql://localhost:3306/jdbc_01?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
db.user=root
db.password=root
db.driver=com.mysql.jdbc.Driver

(4)链接好数据库,去编写一下sql语局 mapper.xml




    
        SELECT * FROM student;
    

注意:id为自定义 resultType为映射类路径 创建包Student类 (有参无参构造,set,get tostring)

package com.shi;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String height;
    private String wight;

    public Student() {
    }

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

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public void setWight(String wight) {
        this.wight = wight;
    }

    public Student(Integer id, String name, Integer age, String height, String wight) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.height = height;
        this.wight = wight;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public String getHeight() {
        return height;
    }

    public String getWight() {
        return wight;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                ", height='" + height + ''' +
                ", wight='" + wight + ''' +
                '}';
    }


}

(5) 配置好配置文件接下来编写MYbatis.java 当然我们要先创建UserMapper一个接口

package com.shi;

import java.util.List;

public interface UserMapper {
    List selectUser();
}

用来存放集合

package com.shi;


import com.alibaba.fastjson.JSON;
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.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

@Controller
public class MyTest {
    @RequestMapping("/student")
    public String Student(Model model) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List list = mapper.selectUser();
        session.close();
//        for (Student user: list){
//            System.out.println(user);
//
//        }
        model.addAttribute("list",list);
        return  "hello";
//        return "hello";
//        return JSON.toJSonString();
//        model.addAttribute("list",list);


    }

}

最后记得创建html页面就可以正常使用了




    
    Title


name

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号