发表于: 2018-01-01 23:12:40
1 472
17.编写DAO,分别JdbcTemplate和Mybatis连接数据库,注意使用JDBCTemplate的时候分离Interface和Imple,使用Mybatis的时候注意理解为什么不需要Impl,注意遵守命名规范。
Mybatis项目结构:
Mybatis-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>
<!--别名-->
<typeAliases>
<package name="how2java.pojo"/>
</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/how2java?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--映射文件地址-->
<mappers>
<mapper resource="how2java/pojo/Category.xml"/>
</mappers>
</configuration>
配置文件Category.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="how2java.pojo">
<insert id="addCategory" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="deleteCategory" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="getCategory" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="updateCategory" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="listCategory" resultType="Category">
select * from category_
</select>
</mapper>
Category实体类
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试:
public class TestMybatis {
public static void main(String[] args) throws IOException {
/*
1. 应用程序找Mybatis要数据
2. mbatis从数据库中找来数据
2.1 通过mybatis-config.xml 定位哪个数据库
2.2 通过Category.xml执行对应的select语句
2.3 基于Catgory.xml把返回的数据库记录封装在Category对象中
2.4 把多个Category对象装在一个Category集合中
3. 返回一个Category集合
*/
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//得到session工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//得到session
SqlSession session = sqlSessionFactory.openSession();
Category c = new Category();
c.setName("新增的category");
//插入 insert 删除delete 获取selectOne 改update
session.insert("addCategory", c);
listAll(session);
session.commit();
session.close();
}
private static void listAll(SqlSession session){
List<Category> cs = session.selectList("listCategory");
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
遇到的问题:
解决方法:
http://blog.csdn.net/u010648555/article/details/70880425
明天计划的事情:由于jdbctemplate属于spring框架的内容,明天大概学习下spring框架的配置方法,然后使用jdbctemplate,完成任务17.
收获:1.使用Mybatis,是第一次使用框架,配置阶段花费了很多时间。创建了很多次项目,maven创建项目还有pom.xml中添加依赖包熟练了。
2.对ORM有了一些理解。
评论