目录结构:
1、配置 Category 文件
这个文件是实体类,与数据表中的数据相对应
package com.how2java.pojo;
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;
}
@Override
public String toString() {
return "Category [id=" + id + ", name=" + name + "]";
}
}
2、配置 CategoryMapper 文件
这个文件是一个接口类,与 Category.xml 中对数据表的操作标签相对应
package com.how2java.mapper;
import java.util.List;
import com.how2java.pojo.Category;
public interface CategoryMapper {
public int add(Category category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
public int count();
}
3、配置 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="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="delete" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="update" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category_
</select>
</mapper>
4、配置 applicationContext 文件
配置数据源,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/db_database24?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>914717</value>
</property>
</bean>
<!--扫描xml文件,最终会扫描到 Category.xml 文件,并把 dataSource(数据源) 传过去-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>
<!--扫描mapper文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>
</beans>
5、配置 MybatisTest 文件
这个文件用来测试====以注解的方式
package com.java.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
// 创建一个接口对象
private CategoryMapper categoryMapper;
@Test
public void testAdd() {
// 创建一个实体类的对象
Category category = new Category();
// 进行赋值
category.setName("new Category");
// 调用接口的 add 方法,从而与 Category.xml 文件中的标签对应
categoryMapper.add(category);
}
@Test
public void testList() {
// 打印 categoryMapper 对象
System.out.println(categoryMapper);
List<Category> cs = categoryMapper.list();
for (Category c : cs) {
System.out.println(c.getName());
}
}
}
运行结果:
总结:
要注意导入的包与javaEE版本的一致性,可以在eclipse上修改JavaEE的版本
本次实例中用到了 Spring 的传参、配置数据源、接口类的实现,用到了 Mybatis 的映射数据操作文件以及注解方式的测试类,对 Spring + Myvbatis 模式有了一个初步的了解,在今后的学习中还需不断复习,不断理解。