create database how2java |
USE how2java; CREATE TABLE category_ ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(32) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; |
USE how2java; INSERT INTO category_ VALUES (null,‘category1‘); INSERT INTO category_ VALUES (null,‘category2‘); |
package com.how2java.pojo; public class Category { private int id; private String name; 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; } } |
<property name= "driver" value= "com.mysql.jdbc.Driver" /> <property name= "username" value= "root" /> <property name= "password" value= "admin" /> |
<typeAliases> < package name= "com.how2java.pojo" /> </typeAliases> |
<mappers> <mapper resource= "com/how2java/pojo/Category.xml" /> </mappers> |
<?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> <typeAliases> < package name= "com.how2java.pojo" /> </typeAliases> <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/how2java?characterEncoding=UTF-8" /> <property name= "username" value= "root" /> <property name= "password" value= "admin" /> </dataSource> </environment> </environments> <mappers> <mapper resource= "com/how2java/pojo/Category.xml" /> </mappers> </configuration> |
namespace= "com.how2java.pojo" |
select * from category_ |
<? 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.how2java.pojo" > < select id = "listCategory" resultType = "Category" > select * from category_ </ select > </ mapper > |
String resource = "mybatis-config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); |
SqlSession session=sqlSessionFactory.openSession(); |
List<Category> cs = session.selectList( "listCategory" ); for (Category c : cs) { System.out.println(c.getName()); } |
package com.how2java; import java.io.IOException; import java.io.InputStream; import java.util.List; 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 com.how2java.pojo.Category; public class TestMybatis { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session=sqlSessionFactory.openSession(); List<Category> cs=session.selectList( "listCategory" ); for (Category c : cs) { System.out.println(c.getName()); } } } |
原文:https://www.cnblogs.com/GGLoner/p/12693457.html