第一步)先用IDEA創(chuàng)建MAVEN項(xiàng)目捎琐,參考上圖步驟玻熙。
第二步)接著填寫grounpId 一般是公司機(jī)構(gòu)的簡寫串远,artifactid 指代項(xiàng)目名稱宏多,后續(xù)步驟默認(rèn)不改一路直點(diǎn)到FInish儿惫。
第三步)打開pom.xml文件,在依賴?yán)锩嫣砑?處依賴伸但,在這里你也可以查找下你本地的MAVEN庫里面版本是什么姥闪,然后填進(jìn)去,如果artifactid有紅字一般是這個庫沒導(dǎo)進(jìn)去
第四步)一般出來結(jié)構(gòu)圖如上砌烁,可能最新版本IDEA沒有resources筐喳,那么需要手動添加一個文件夾,再把它右鍵設(shè)置標(biāo)識為resources root.
第五步)新建一個實(shí)體類Person函喉。具體代碼如下(數(shù)據(jù)庫自己另行建一個對應(yīng)字段的表)
public class Person {
private int id;
private String userName ;
private int age ;
private String mobilePhone ;
public Person(){}
public Person(int id,String userName, int age, String mobilePhone) {
this.id = id;
this.userName = userName;
this.age = age;
this.mobilePhone = mobilePhone;
}
public String getUserName() {
return userName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
", age=" + age +
", mobilePhone='" + mobilePhone + '\'' +
'}';
}
}
第六步)有了實(shí)體和數(shù)據(jù)庫表以后當(dāng)然是用Mybatis關(guān)聯(lián)映射避归。我們新建2個配置文件到resources,一個是數(shù)據(jù)庫配置信息,另一個是Mybatis配置信息管呵。
我的數(shù)據(jù)庫配置mysql.properties參考如下:
driver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/nobody
username:root
password:123456
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>
<properties resource="mysql.properties"></properties>
<typeAliases>
<typeAlias type="com.luban.model.Person" alias="Person"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/Person.xml"/>
</mappers>
<!-- Continue going here -->
</configuration>
這個配置有個主意地方就是 <typeAliases>不要放到environments 的下面梳毙,不然會報莫名錯誤,原本我是想放到mappers 上面好看點(diǎn)但是發(fā)現(xiàn)報錯捐下,具體原因可能是讀取時候有順序账锹。
typeAlias 里面type是指代項(xiàng)目中的類的全名,alias是別名坷襟。
mapper是對應(yīng)的映射位置(就是相對于resources文件夾)奸柬。中間的數(shù)據(jù)庫配置用了占位符,讀取剛才數(shù)據(jù)庫配置文件婴程。
第七步)數(shù)據(jù)庫映射完畢后接著書寫數(shù)據(jù)庫操作腳本Person.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="com.luban">
<insert id="insertPerson" parameterType="Person" >
INSERT INTO PERSON(ID,USERNAME,AGE,MOBILEPHONE)VALUES (#{id},#{userName},#{age},#{mobilePhone})
</insert>
<select id="queryById" parameterType="int" resultType="Person">
SELECT * FROM PERSON WHERE ID=#{id}
</select>
<update id="updatePerson">
UPDATE PERSON SET USERNAME=#{userName},AGE=#{age},MOBILEPHONE=#{mobilePhone} WHERE ID=#{id}
</update>
</mapper>
我的數(shù)據(jù)庫表如下
第八步)編寫一個測試方法廓奕,具體如下:
public class App
{
public static void main( String[] args )
{
String resources="mybatis-config.xml";
Reader reader=null;
try {
reader=Resources.getResourceAsReader(resources);
SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);
SqlSession session=sqlMapper.openSession();
Person user=session.selectOne("queryById",1);
System.out.println(user.getUserName());
session.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后是項(xiàng)目整體結(jié)構(gòu)圖:
啟動項(xiàng)目查看控制臺信息如下:
完畢!