Mybatis3.4.6
http://www.mybatis.org/mybatis-3/zh/getting-started.html
Eclipse创建maven工程:
步骤一:添加依赖
org.mybatis
mybatis
3.4.6
步骤二:添加MySql驱动
mysql
mysql-connector-java
5.1.41
步骤三:添加配置文件mybatis-config.xml、jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisstd
jdbc.username=root
jdbc.password=1234
步骤四:Eclipse MyBatis Generator插件安装
步骤五:
/DDL 信息/------------
CREATE TABLE app_user
(
id
int(10) unsigned zerofill NOT NULL AUTO_INCREMENT,
userName
varchar(50) DEFAULT NULL,
password
varchar(50) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
步骤六:
package cn.qchcloud.mybatisstd;
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;
import cn.qchcloud.mybatisstd.domain.AppUser;
/**
- Hello world!
*/
public class App
{
public static void main( String[] args )
{
System.out.println( “Hello World!” );
String resource=“mybatis-config.xml”;
try {
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
System.out.println(sqlSessionFactory);
AppUser user=new AppUser();
user.setUsername("test");
user.setPassword("123456");
SqlSession sqlSession=sqlSessionFactory.openSession();
sqlSession.insert("insert", user);
sqlSession.commit();
sqlSession.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}