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());
    

      

  • 相关阅读:
    C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
    FlexPaper在线文档分享(转载)
    winform中屏蔽对标题栏的操作
    【转】海量数据查询优化
    [转帖]用Reflector和FileDisassembler配合反编译.net Windows程序
    关于中缀表达式和逆波兰表达式(终结篇)
    jQuery教程基础篇之强大的选择器(层次选择器)
    模版方法(Template Method)
    VS2008新建项目时出现“此安装不支持该项目类型”
    并行计算相关文章
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/5266468.html
Copyright © 2011-2022 走看看