关闭

SqlSession的获取及对数据库执行插入操作

1363人阅读 评论(0) 收藏 举报
分类:

项目架构如图:


插入代码:


package com.boge;
import java.io.IOException;
import java.io.Reader;
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.boge.mapper.UserMapper;
import com.boge.pojo.User;

public class Insert {
	public static void main(String[] args) throws IOException, InterruptedException  {
		Reader reader = Resources.getResourceAsReader("mybatis.xml"); 
        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);  
        SqlSession session = sf.openSession(true); 
        UserMapper userMapper = session.getMapper(UserMapper.class);
        for (int i = 0; i < 10; i++) {
        	 System.out.println("*****"+i);
        	 User user = new User();
//        	 user.setId(100+i);//id是自增的
             user.setName("******嘿嘿"+i);
             user.setAddress("山东菏泽");
             user.setCode("274000");
             user.setNote("低调");
             user.setSex("汉子");
             userMapper.insert(user);
             Thread.sleep(200);
		}
	}
}


mybatis.xml:

<?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/boge" />
            <property name="username" value="root"/>
            <property name="password" value="7758521song"/>
            </dataSource>
        </environment>
    </environments>

	<mappers>
        <!-- 数据标准化 -->
         <mapper resource="com/boge/xml/UserMapper.xml" />
	</mappers>
</configuration>

用于生成User、UserMapper、以及UserMapper.xml的generatorConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration >
<classPathEntry location="C:/Users/Bo/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" /> 
  <context id="user" >
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/boge"
      userId="root" password="7758521song">
    </jdbcConnection>
   <javaModelGenerator targetPackage="com.boge.pojo" targetProject="Pro/src/main/java">
    </javaModelGenerator>
    
    <sqlMapGenerator targetPackage="com.boge.xml" targetProject="Pro/src/main/java">
    </sqlMapGenerator>
   <javaClientGenerator type="XMLMAPPER" targetPackage="com.boge.mapper"
      targetProject="Pro/src/main/java">
    </javaClientGenerator>
    
    <table schema="boge" tableName="user" domainObjectName="User">
    </table>
  </context>
</generatorConfiguration>


在pom中加入依赖,pom.xml如下:

<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>Boge</groupId>
	<artifactId>com</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>com</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	        <dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.34</version>
		</dependency>
	</dependencies>
</project>


项目下载:https://github.com/MyHeartWillFoever/SqlSession.git




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6754次
    • 积分:150
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:10篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论