本文转载至http://blog.csdn.net/mahoking
本文将介绍MyBatis的环境搭建,本例使用的MyBatis的版本为mybatis-3.2.8.jar。下载地址为:http://download.csdn.net/detail/ma_hoking/8380541。本例使用的数据库为MySQL数据。具体的安装操作请参考:http://blog.csdn.net/mahoking/article/details/42921511。
【转载使用,请注明出处:http://blog.csdn.net/mahoking】
首先创建Java项目MyBatisTest。导入项目需要的jar文件mybatis-3.2.8.jar,由于本例需要连接MySQL数据库,所以需要导入mysql-connector-java-5.1.7-bin.jar。
创建User实体对象
- public class User {
- private Integer id ;
- private String userName ;
- private String password;
- /*
- 必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,
- 在查询数据库时,将不能反射构造出User实例
- /
- public User() {
- }
- public User(String userName, String password) {
- super();
- this.userName = userName;
- this.password = password;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
public class User {
private Integer id ;
private String userName ;
private String password;
/**
* 必须要有这个无参构造方法,不然根据UserMapper.xml中的配置,
* 在查询数据库时,将不能反射构造出User实例
*/
public User() {
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
创建configuration.xml,该文件类似Hibernate.cfg.xml文件,可以称之为主配置文件。导入dtd文件。
- <!DOCTYPE configuration PUBLIC
- ”-//mybatis.org//DTD Config 3.0//EN”
- ”http://mybatis.org/dtd/mybatis-3-config.dtd“>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
编写configuration.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>
- <typeAliases>
- <!– 注册实体Bean –>
- <typeAlias type=“com.mahaochen.mybatis.domain.User” alias=“User”/>
- </typeAliases>
- <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://127.0.0.1:3306/mhc”/>
- <property name=“username” value=“root”/>
- <property name=“password” value=“root”/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource=“com/mahaochen/mybatis/domain/User.xml”/>
- </mappers>
- </configuration>
<?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>
<typeAliases>
<!-- 注册实体Bean -->
<typeAlias type="com.mahaochen.mybatis.domain.User" alias="User"/>
</typeAliases>
<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://127.0.0.1:3306/mhc"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mahaochen/mybatis/domain/User.xml"/>
</mappers>
</configuration>
从上文可知,引入了User.xml,该文件类似于Hibernate中的*.hbm.xml文件。那我们现在编写User.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“>
- <!– 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”–>
- <mapper namespace=“com.mahaochen.mybatis.interfaces.UserMapper”>
- <!– 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候会报错 –>
- <!– 添加操作 这里sql结尾不能加分号,否则报错误 –>
- <insert id=“insertUser” parameterType=“User”>
- <![CDATA[
- insert into mhc_user(userName,password) values(#{userName},#{password})
- ]]>
- </insert>
- <!– 删除操作 –>
- <delete id=“deleteUser” parameterType=“String”>
- <![CDATA[
- delete from mhc_user where userName=#{userName}
- ]]>
- </delete>
- <!– 更新操作 –>
- <update id=“updateUser” parameterType=“User”>
- <![CDATA[
- update mhc_user set userName=#{userName},password=#{password} where id=#{id}
- ]]>
- </update>
- <!– 查询操作 –>
- <select id=“selectUser” parameterType=“int” resultType=“User”>
- <![CDATA[
- select * from mhc_user where id = #{id} ;
- ]]>
- </select>
- </mapper>
<?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“>
<!– 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry”–>
<mapper namespace=”com.mahaochen.mybatis.interfaces.UserMapper”>
<!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候会报错 -->
<!-- 添加操作 这里sql结尾不能加分号,否则报错误 -->
<insert id="insertUser" parameterType="User">
<![CDATA[
insert into mhc_user(userName,password) values(#{userName},#{password})
]]>
</insert>
<!-- 删除操作 -->
<delete id="deleteUser" parameterType="String">
<![CDATA[
delete from mhc_user where userName=#{userName}
]]>
</delete>
<!-- 更新操作 -->
<update id="updateUser" parameterType="User">
<![CDATA[
update mhc_user set userName=#{userName},password=#{password} where id=#{id}
]]>
</update>
<!-- 查询操作 -->
<select id="selectUser" parameterType="int" resultType="User">
<![CDATA[
select * from mhc_user where id = #{id} ;
]]>
</select>
</mapper>
上文中出现com.mahaochen.mybatis.interfaces.UserMapper将在下文中介绍。 MyBatis与Hibernate有一定的相似之处,在学习Hibernate时,博主介绍了HibernateUtil这个工具对象用于管理SessionFactory和Session。对于MyBatis,这里我们也需要创建一个MyBatisUtil对象用于管理SqlSessionFactory和SqlSession。
- /
- 提供获取SqlSessionFactory对象或管理Sesison的操作
- @author mahc
- /
- public class MyBatisUtil {
- private static SqlSessionFactory ssf;
- static{
- String resource = ”configuration.xml”;
- Reader reader;
- try {
- reader = Resources.getResourceAsReader(resource);
- ssf = new SqlSessionFactoryBuilder().build(reader);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /
- 获取SqlSessionFactory
- @return
- /
- public static SqlSessionFactory getSqlSessionFactory(){
- return ssf;
- }
- /
- 关闭Session
- @param session
- /
- public static void closeSession(SqlSession session){
- if(null!=session){
- session.close();
- }
- }
- }
/
* 提供获取SqlSessionFactory对象或管理Sesison的操作
* @author mahc
*
*/
public class MyBatisUtil {
private static SqlSessionFactory ssf;
static{
String resource = "configuration.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resource);
ssf = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取SqlSessionFactory
* @return
*/
public static SqlSessionFactory getSqlSessionFactory(){
return ssf;
}
/**
* 关闭Session
* @param session
*/
public static void closeSession(SqlSession session){
if(null!=session){
session.close();
}
}
}
MyBatis环境搭建就此完成,具体的操作数据库的增删改查操作将在下文中介绍。敬请期待!!点击查看下文!
【转载使用,请注明出处:http://blog.csdn.net/mahoking】