Mybatis3+SpringMVC
项目结构+jar包
person.java
package main.bean;
public class person {
private int id;
private String name;
private int age;
private String sex;
//不要缺少无参构造函数
public person() {
super();
// TODO Auto-generated constructor stub
}
public person(int id, String name, int age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "person [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
personMapper.java(映射接口类需与对应的映射配置文件同名,否则框架加载不到。personMapper.xml)
package main.Dao;
import java.util.List;
import main.bean.person;
public interface personMapper {
//相应的方法名需与对应在xml文件中的sql语句的id相同,比如对应在personMapper.xml中的<insert id="add">
int add(person p);
// boolean update(person p);
// boolean delete(int id);
// List<person> selectPerson();
}
personMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="main.Dao.personMapper">
<insert id="add" parameterType="person">
insert into person
(
name,
age,
sex
)
values
(
#{name},
#{age},
#{sex}
)
</insert>
</mapper>
beans.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource:引用数据源
typeAliasesPackage:制定映射类package,mapper.xml中可以直接使用类名作为类型别名
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="main.bean"></property>
</bean>
<!--
3. mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer
sqlSessionFactory:引用sqlsessionfactory
basePackage:指定sql映射文件/接口所在的包(自动扫描)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="main.Dao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
4. 事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5. 使用声明式事务
transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
test.java
package main.test;
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 main.Dao.personMapper;
import main.bean.person;
//该出利用JUnit的Spring测试框架(spring-test-4.1.6.RELEASE.jar)
@RunWith(SpringJUnit4ClassRunner.class)
//加载配置文件。在SpringMVC+mybatis的web项目中,在web.xml中配置加载相应的projectname-config.xml即可
@ContextConfiguration("/beans.xml")
public class test {
@Autowired
public personMapper mapper;
@Test
public void test(){
person p = new person(1,"wangwu",26,"M");
int result = mapper.add(p);
System.out.println(result);
}
}
针对具体的SpringMVC+Mybatis的web项目,bean.xml中的配置可以写到projectname-config.xml中,然后在web.xml中加载到SpringMVC默认的分配器中。
或者参考我的简单的个人博客项目代码:https://github.com/DreamFinderHou/PersonalBlog.git