一.安装MySql
首先下载Mysql,及其管理工具(可图形化操作)mysqladministrator
在catalogs中创建新的数据库和表,注,如要中文数据,在charset中选utf-8或gbk
如卸载后重装要把所有注册表中内容及c:盘用户下所有mysql文件夹都删除
二.文件结构
需要使用的Jar包:mybatis-3.0.5.jar(mybatis核心包)
在创建表后
三.搭建MyBatis环境
Configuration.xml
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig 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" />
<!--iotek是数据库名字-->
<property name="url"value="jdbc:mysql://127.0.0.1:3306/iotek?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username"value="root" />
<property name="password"value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/promise/maps/Account.xml"/>
</mappers>
</configuration>
SessionFactory.java
public class SessionFactory{
private static SqlSessionFactory sqlSessionFactory = null;
static {
//配置使用mysql数据库
String resource = "com/promise/util/configuration.xml";
Reader reader = null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
创建实例
Account.java
public class Account
{
private String firstName;
private int id;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
}
创建sql语句
Account.xml
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTDMapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.promise.mpas">
<select id="selectAccountById" parameterType="int" resultType="com.promise.bean.Account">
<!-- account是表名 -->
select * from account where id =#{id}
</select>
<select id="selectAccountByIdAndName"parameterType="com.promise.bean.Account" resultType="com.promise.bean.Account">
select * from account
<where>
<if test= "id !=null and id != -1 ">
AND id = #{id}
</if>
<if test= "firstName!= null">
AND firstName = #{firstName}
</if>
<if test= "gender!= null">
AND gender = #{gender}
</if>
</where>
</select>
<insert id="insertAccount"parameterType="com.promise.bean.Account">
insertinto account(id, firstName, lastName, gender)
value(#{id}, #{firstName}, #{lastName},#{gender})
</insert>
<update id="updateAccount"parameterType="com.promise.bean.Account">
update account
set
firstName=#{firstName},
lastName=#{lastName},
gender=#{gender}
where id=#{id}
</update>
<delete id="deleteAccount"parameterType="int">
delete from account where id=#{id}
</delete>
</mapper>
创建使用的接口
AccountDao.java
public class AccountDao
{
public Account query(Integer id) {
SqlSession session = SessionFactory.getSqlSessionFactory().openSession();
Account account = null;
try {
account = (Account)session.selectOne("com.promise.mpas.selectAccountById", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return account;
}
public Account query(Account acc) {
SqlSession session = SessionFactory.getSqlSessionFactory().openSession();
Account account = null;
try {
account = (Account)session.selectOne("com.promise.mpas.selectAccountByIdAndName", acc);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return account;
}
public void insert(Account account) {
SqlSession session = SessionFactory.getSqlSessionFactory().openSession();
try {
session.insert("com.promise.mpas.insertAccount", account);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.commit();
session.close();
}
return ;
}
public void update(Account account) {
SqlSession session = SessionFactory.getSqlSessionFactory().openSession();
try {
session.update("com.promise.mpas.updateAccount", account);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.commit();
session.close();
}
return ;
}
public void delete(int id) {
SqlSession session = SessionFactory.getSqlSessionFactory().openSession();
try {
session.delete("com.promise.mpas.deleteAccount", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.commit();
session.close();
}
return ;
}
}
测试
Test.java
public class Test {
public static void main(String[] args) {
Test test = new Test();
Account user = test.quareByAccount();
System.out.println(user.getId());
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getGender());
}
public void insert()
{
AccountDao accountDao = new AccountDao();
Account user = new Account();
user.setFirstName("啊3");
user.setLastName("bbb3");
user.setGender((byte) 1);
accountDao.insert(user);
}
public void update()
{
AccountDao accountDao = new AccountDao();
Account user = new Account();
user.setId(1);
user.setFirstName("a2");
user.setLastName("啊啊2");
user.setGender((byte) 1);
accountDao.update(user);
}
public void delete(int id)
{
AccountDao accountDao = new AccountDao();
accountDao.delete(id);
}
public Account quareById(int id)
{
AccountDao accountDao = new AccountDao();
return accountDao.query(id);
}
public Account quareByAccount()
{
AccountDao accountDao = new AccountDao();
Account user = new Account();
user.setId(-1);
// user.setId(1);
// user.setFirstName("a2");
user.setGender((byte) 0);
return accountDao.query(user);
}
}
Web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>