zoukankan      html  css  js  c++  java
  • java数据库访问(四)—Mybatis

    前述的几种java访问数据库的方式:

    • jdbc是最原始的方式,使用比较繁琐;
    • JdbcTemplate方式相对方便性有一些提高,但在工程应用中仍然很不方便。

    接下来继续记录使用持久化框架来进行数据库操作,本文记录使用Mybatis进行记录的的简单示例:

    1、定义

    官网的定义:

    MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. MyBatis can use simple XML or Annotations for configuration and map primitives, Map interfaces and Java POJOs (Plain Old Java Objects) to database records.

    2、依赖

    spring中使用mybatis需要添加如下依赖(pom.xml):

      <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.6</version>
            </dependency>

    3、配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-2.5.xsd  
         http://www.springframework.org/schema/beans
         https://www.springframework.org/schema/beans/spring-beans.xsd">
         <bean id="dataSource"
         class="org.apache.commons.dbcp2.BasicDataSource"
         destroy-method="close">
         <property name="url" value="jdbc:mysql://localhost:3306/test"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         </bean>
         <bean id="sqlSessionFactory"
         class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
         </bean>
         <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="com.test.database.mybatis"/>
          <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
         </bean>
    </beans>

    4、sql映射文件

    <?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.test.database.mybatis.TestDao">
    <select id="getAll" resultType="com.test.database.User">
    select * from user
    </select>
    </mapper>

    5、接口定义

    package com.test.database.mybatis;
    
    import java.util.List;
    
    import com.test.database.User;
    
    public interface TestDao {
        
        public List<User> getAll();
    
    }

    User定义

    package com.test.database;
    
    public class User {
        
        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 "User [id=" + id + ", name=" + name + ", password=" + password + "]";
        }
        
        
    
    }

    6、测试类

    package com.test;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.test.database.mybatis.TestDao;
    
    public class Main {
    
        public static void main(String[] args) throws BeansException, Exception {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-context*.xml");
            //测试mybatis
            System.out.println(context.getBean(TestDao.class).getAll());
        }
    }

    7、测试结果

    [User [id=1, name=tim, password=tim], User [id=2, name=park, password=park]]
  • 相关阅读:
    Unity—Json2
    Unity—Json1
    windows 命令行报错:file(s) not in client view
    winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条
    windows 命令行使用p4
    winform listbox增加鼠标双击事件
    lua5.3+luasocket
    centos 安装glide工具(golang)笔记
    centos 安装golang笔记
    centos安装redis,并设置开机自动启动项
  • 原文地址:https://www.cnblogs.com/silenceshining/p/12562608.html
Copyright © 2011-2022 走看看