日期:2014-05-17 浏览次数:20972 次
<?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>
<typeAlias alias="User" type="mvc.web.controller.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://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mvc/web/controller/UserMapper.xml"/>
</mappers>
</configuration>
<?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="mvc.web.controller.UserMapper">
<cache />
<select id="selectUser" parameterType="String" resultType="User">
select * from u where name = #{name}
</select>
<delete id="deleteUser" parameterType="User">
delete from u where name = #{name}
</delete>
</mapper>
package mvc.web.controller;
public interface UserMapper {
User selectUser(String name);
void deleteUser(User user);
}
package mvc.web.controller;
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String name;
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package mvc.web.controller; import java.io.IOException; import java.io.InputStream; 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; public class Test_Mybatis { @Test public void t1(){ String resource = "mvc/web/controller/mybatis.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch