zoukankan      html  css  js  c++  java
  • 一个最小mybatis

    项目结构

    package hello;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class App 
    {
        public static void main( String[] args ) throws IOException
        {
        	String resource = "hello/config.xml";
        	InputStream inputStream = Resources.getResourceAsStream(resource);
        	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        	SqlSession session = sqlSessionFactory.openSession();
        	User user = (User)session.selectOne("hello.UserMapper.selectOne");
        	System.out.println(user.getName());
        }
    }
    

    实体类

    package hello;
    
    public class User
    {
    	private String name;
    	private int age;
    
    	public String getName()
    	{
    		return name;
    	}
    
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    
    	public int getAge()
    	{
    		return age;
    	}
    
    	public void setAge(int age)
    	{
    		this.age = age;
    	}
    
    }
    

      

    <?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.jdbc.Driver" />
    				<property name="url" value="jdbc:mysql://localhost:3306/boot" />
    				<property name="username" value="root" />
    				<property name="password" value="123456" />
    			</dataSource>
    		</environment>
    	</environments>
    	<mappers>
    		<mapper resource="hello/UserMapper.xml" />
    	</mappers>
    </configuration>
    

    映射文件

    <?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="hello.UserMapper">
    	<select id="selectOne" resultType="hello.User">
    		select * from user;
    	</select>
    </mapper>
    

    pom

    <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>test.demo</groupId>
    	<artifactId>mybatis</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>mybatis</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<dependencies>
    		<!-- mybatis -->
    		<dependency>
    			<groupId>org.mybatis</groupId>
    			<artifactId>mybatis</artifactId>
    			<version>3.2.3</version>
    		</dependency>
    		<!-- mysql -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.38</version>
    		</dependency>
    	</dependencies>
    </project>
    

    首先以流的信息将数据库连接信息读取进来,生成到数据库的连接(SqlSession)

    String resource = "hello/config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    
     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
     SqlSession session = sqlSessionFactory.openSession();
    

    进行数据库操作

        	User user = (User)session.selectOne("hello.UserMapper.selectOne");
        	System.out.println(user.getName());
    

      

  • 相关阅读:
    Web性能压力测试工具之WebBench详解
    Request Header Or Cookie Too Large
    手动删除SVCH0ST.EXE的方法
    微信非80端口网页授权
    微信公众号开发80端口映射解决方案 推荐
    解决微信访问 80 端口的限制问题
    Windows查看所有的端口及端口对应的程序
    Windows环境下,用netstat命令查看某个端口号是否占用
    价值观--我的总结
    价值是事物由人(基于自己的需要和获取的难度而)作出的评价而存在的属性
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/5266468.html
Copyright © 2011-2022 走看看