Lightroom Classic 教程,如何在 Lightroom 中使用引導式修復透檢視?
Netkiller Java 手札
關於《Netkiller Java 手札》
作者2002年開始在專案中使用Java,各種原因沒有留下Java文件,2015因工作需要重新拾起Java並整理本文件。
我的系列文件
程式語言
Netkiller Architect 手札 |
Netkiller Developer 手札 |
Netkiller PHP 手札 |
Netkiller Python 手札 |
Netkiller Testing 手札 |
Netkiller Cryptography 手札 |
---|---|---|---|---|---|
Netkiller Perl 手札 |
Netkiller Docbook 手札 |
Netkiller Project 手札 |
Netkiller Java 手札 |
Netkiller DevOps 手札 |
作業系統
Netkiller Linux 手札 |
Netkiller Debian 手札 |
Netkiller CentOS 手札 |
Netkiller FreeBSD 手札 |
Netkiller Shell 手札 |
Netkiller Security 手札 |
---|---|---|---|---|---|
Netkiller Web 手札 |
Netkiller Monitoring 手札 |
Netkiller Storage 手札 |
Netkiller Mail 手札 |
Netkiller Multimedia 手札 |
資料庫
Netkiller Database 手札 |
Netkiller PostgreSQL 手札 |
Netkiller MySQL 手札 |
Netkiller NoSQL 手札 |
Netkiller LDAP 手札 |
---|
網路裝置及其他
Netkiller Network 手札 |
Netkiller Cisco IOS 手札 |
Netkiller H3C 手札 |
Netkiller Amateur Radio 手札 |
---|
您可以使用iBook閱讀當前文件
第 7 章 MyBatis
目錄
- 7.1. Mybatis 入門
http://blog.mybatis.org/
7.1. Mybatis 入門
建立資料庫與表並插入測試資料
CREATE DATABASE `mybatis` /*!40100 COLLATE 'utf8_general_ci' */; CREATE USER 'mybatis'@'192.168.%' IDENTIFIED BY 'mybatis'; GRANT USAGE ON *.* TO 'mybatis'@'192.168.%'; GRANT SELECT, EXECUTE, SHOW VIEW, ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE TEMPORARY TABLES, CREATE VIEW, DELETE, DROP, EVENT, INDEX, INSERT, REFERENCES, TRIGGER, UPDATE, LOCK TABLES ON `mybatis`.* TO 'mybatis'@'192.168.%' WITH GRANT OPTION; FLUSH PRIVILEGES; SHOW GRANTS FOR 'mybatis'@'192.168.%'; CREATE TABLE IF NOT EXISTS `user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `age` int(10) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERT INTO `user` (`id`, `name`, `age`) VALUES (1, 'Neo', 35), (2, 'Jerry', 36);
Maven pom.xml 中加入依賴包
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
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>MyBatis</groupId>
<artifactId>MyBatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/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://192.168.6.1:3306/mybatis" />
<property name="username" value="mybatis" />
<property name="password" value="mybatis" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="cn/netkiller/mapping/userMapping.xml" />
</mappers>
</configuration>
src/cn/netkiller/mapping/userMapping.xml
<?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="cn.netkiller.mapping.UserMapping">
<select id="getUser" parameterType="String" resultType="cn.netkiller.model.User">
select * from user where id=#{id}
</select>
</mapper>
resultType 檔案
package cn.netkiller.model;
public class User {
private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
測試程式碼
package cn.netkiller.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.netkiller.model.*;
public class Tests {
public static void main(String[] args) {
// TODO Auto-generated method stub
String resource = "mybatis.xml";
InputStream is = Tests.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = sessionFactory.openSession();
String statement = "cn.netkiller.mapping.UserMapping.getUser";// 對映sql的標識字串
User user = session.selectOne(statement, "2");
System.out.println(user.toString());
}
}