今天終於把它弄清楚了,剛剛學java,挺費勁呀。
在mysql中創建一個數據庫mygps,表名爲test(id,age,username,password)
(1)先引用mybatis-3.0.2.jar,mysql-connector-java-5.0.8-bin.jar
(2)配置連接數據庫xml文件取名叫config.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/mygps"/>//數據庫名爲mypgs
<property
name="username" value="root"/>
<property
name="password"
value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper
resource="org/mybatis/test/testmapper.xml"/>//寫sql語句的xml
</mappers>
</configuration>
(3)配置sql語句的xml取名叫testmapper.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="testmapper">
<select id="selectbyid"
parameterType="int"
resultType="org.mybatis.test.test">//test爲一個類,用於設置與數據庫有關的數據類型
select * from test where
test.id=#{id}
</select>
<insert id="insertvalues"
parameterType="org.mybatis.test.test">
insert into test (id,username,password,age) values
(#{id},#{username},#{password},#{age})
</insert>
<update id="updatevalue"
parameterType="org.mybatis.test.test">
update test set username=#{username} where id=#{id}
</update>
</mapper>
(4)取出數據庫的連接取名叫util 是一個類
package org.mybatis.test;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class util {
public static SqlSessionFactory sqlmapper=null;
static
{
String
rs="org/mybatis/test/config.xml";//爲第二步中的配置文件名
Reader reader=null;
try
{
reader=Resources.getResourceAsReader(rs);
}
catch(IOException e)
{
e.printStackTrace();
}
sqlmapper=new
SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory
getSqlSessionFactory()
{
return sqlmapper;
}
}
(5)執行sql語句取名叫getTest 是一個類
package org.mybatis.test;
import org.mybatis.test.util;
import org.mybatis.test.test;
import org.apache.ibatis.session.SqlSession;
public class getTest {
public test getvalue(int
id)
{
SqlSession
session=
util.getSqlSessionFactory().openSession();
test ts=null;
try
{
ts=(test)session.selectOne("testmapper.selectbyid",id);//爲第三步中配置文件中的select
id
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
session.close();
}
return ts;
}
public void insertvalue(int id, String
username,String password,int age)
{
SqlSession session=
util.getSqlSessionFactory().openSession();
test ts=new test();
ts.SetID(id);
ts.SetUserName(username);
ts.SetPassWord(password);
ts.SetAge(age);
try
{
session.selectOne("testmapper.insertvalues",ts);)//爲第三步中配置文件中的insert
id
}
catch(Exception
e)
{
e.printStackTrace();
}
finally
{
session.close();
}
}
public void updatevalue(int id,String
username)
{
SqlSession session=
util.getSqlSessionFactory().openSession();
test ts=new test();
ts.SetID(id);
ts.SetUserName(username);
try
{
session.selectOne("testmapper.updatevalue",ts);)//爲第三步中配置文件中的update
id
}
catch(Exception
e)
{
e.printStackTrace();
}
finally
{
session.close();
}
}
}
(6)爲數據源編寫test類
package org.mybatis.test;
public class test {
private int id;
private int age;
private String username;
private String password;
public int getID()
{
return this.id;
}
public void SetID(int id)
{
this.id=id;
}
public int getAge()
{
return this.age ;
}
public void SetAge(int age)
{
this.age=age;
}
public String getUserName()
{
return this.username ;
}
public void SetUserName(String username)
{
this.username =username;
}
public String getPassWord()
{
return this.password ;
}
public void SetPassWord(String password)
{
this.password =password;
}
}