pom 文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
.................
.................
.................
<packaging>jar</packaging>
<dependencies>
<!--mysql数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--log4j日志依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--调试junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
-------------------------------------------------------------------------------------------------------------------
mybatis 的config配置文件
<?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>
<!--引入属性的配置文件jdbcConfig.properties jdbcConfig.properties需要放在resources的根目录下-->
<properties resource="jdbcConfig.properties">
</properties>
<!--配置环境-->
<environments default="mysql">
<!--配置mysql的环境-->
<environment id="mysql">
<!--配置事务的类型-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源(连接池)-->
<dataSource type="POOLED">
<!--配置连接数据库的四个基本信息-->
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<!--指向数据库操作的mapper文件-->
<mapper resource="com/sealong/dao/IRecipeDao.xml"></mapper>
</mappers>
</configuration>
---------------------------------------------------------------------------------------------------------------------
jdbcConfig.properties配置文件内容
#mysql数据库驱动
driver=com.mysql.jdbc.Driver
#数据库地址
url=jdbc:mysql://localhost:3306/his_db
#用户名
username=root
#密码
password=12345678
------------------------------------------------------------------------------------------------------------------------
mybatis的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">
<!--指向所对应的dao文件-->
<mapper namespace="com.sealong.dao.IRecipeDao">
<!--id 对应的方法名 resultType 所需返回的对象名-->
<select id="findAll" resultType="com.sealong.domain.Recipe">
select * from recipe
</select>
<update id="updateRecipe" parameterType="com.sealong.domain.Recipe">
update recipe set form = #{form} where id = #{id}
</update>
<insert id="saveRecipe" parameterType="com.sealong.domain.Recipe">
insert into recipe(code,parent,name,alias,form,method,effect,dosageUsage,summary) values
(#{code},#{parent},#{name},#{alias},#{form},#{method},#{effect},#{dosageUsage}, #{summary})
</insert>
</mapper>
-------------------------------------------------------------------------------------------------------------------------
dao文件内容
package com.sealong.dao;
import com.sealong.domain.Recipe;
import java.util.List;
public interface IRecipeDao {
/**
* 查找所有处方
* @return
*/
List<Recipe> findAll();
/**
* 添加处方
* @param recipe
*/
void saveRecipe(Recipe recipe);
/**
* 更新处方
* @param recipe
*/
void updateRecipe(Recipe recipe);
}
---------------------------------------------------------------------------------------------------------------------------
dao文件相对应的对象文件
package com.sealong.domain;
public class Recipe {
private Integer id;
private String code;
private String parent;
private String name;
private String alias;
private String form;
private String method;
private String effect;
private String dosageUsage;
private String summary;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getEffect() {
return effect;
}
public void setEffect(String effect) {
this.effect = effect;
}
public String getDosageUsage() {
return dosageUsage;
}
public void setDosageUsage(String dosageUsage) {
this.dosageUsage = dosageUsage;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Recipe{");
sb.append("id=").append(id);
sb.append(", code='").append(code).append('\'');
sb.append(", parent='").append(parent).append('\'');
sb.append(", name='").append(name).append('\'');
sb.append(", alias='").append(alias).append('\'');
sb.append(", form='").append(form).append('\'');
sb.append(", method='").append(method).append('\'');
sb.append(", effect='").append(effect).append('\'');
sb.append(", dosageUsage='").append(dosageUsage).append('\'');
sb.append(", summary='").append(summary).append('\'');
sb.append('}');
return sb.toString();
}
}
---------------------------------------------------------------------------------------------------------------------------
dao测试类文件
package com.sealong.dao;
import com.sealong.domain.Recipe;
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 org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class IRecipeDaoTest {
@Test
public void findAll() throws Exception{
//第一步:获取配置文件
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
//第二步:创建SqlSessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
//第三步:通过SqlSessionFactory来创建SqlSession
SqlSession sqlSession = factory.openSession();
//第四步:通过sqlSession创建相应的dao
IRecipeDao dao = sqlSession.getMapper(IRecipeDao.class);
//第五步:执行dao中的方法
List<Recipe> list = dao.findAll();
for (Recipe recipe : list) {
System.out.println(recipe);
}
//第六步:释放资源
sqlSession.close();
stream.close();
}
}
执行结果