06spring、mybatis整合
2019-08-01 本文已影响0人
RobertLiu123
1、添加依赖
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hys.web.project</groupId>
<artifactId>hys_demo_ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hys_demo_ssm</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- 数据源 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<!-- Mybatis3.4.1 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- spring整合mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!-- Spring-4.2.0 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>
2、spring核心配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.neusoft.**.service,com.neusoft.**.dao" />
<!-- 引入外置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties" />
</bean>
<!--数据库连接池配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name = "url" value="${jdbc.url}" />
<property name = "username" value="${jdbc.username}" />
<property name = "password" value="${jdbc.password}" />
</bean>
<!-- spring和MyBatis完美整合 -->
<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定数据源 -->
<property name = "dataSource" ref="dataSource" />
<!-- 具体指定xml文件,可不配 -->
<property name = "configLocation" value="classpath:mybatis-config.xml" />
<!-- 自动扫描mapping.xml文件,**表示迭代查找 ,,也可在mybatis-config.xml中单独指定xml文件 -->
<property name = "mapperLocations" value="classpath:com/neusoft/**/dao/*.xml" />
</bean>
<!-- 自动扫描com/hys/app/**/dao下的所有dao接口,并实现这些接口,可直接在程序中使用dao接口,不用再获取sqlsession对象 -->
<bean class ="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- basePackage 属性是映射器接口文件的包路径。 你可以使用分号或逗号 作为分隔符设置多于一个的包路径 -->
<property name = "basePackage" value="com/neusoft/**/dao" />
<!-- 因为会自动装配 SqlSessionFactory和SqlSessionTemplate 所以没 有 必 要 去 指 定 SqlSessionFactory或
SqlSessionTemplate 因此可省略不配置; 但是,如果你使 用了一个 以上的 DataSource,那么自动装配可能会失效。 这种
情况下,你可以使用sqlSessionFactoryBeanName或sqlSessionTemplateBeanName属性来设置正确的 bean名称来使用; -->
<!-- <property name = "sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
</bean>
<!-- 事务管理器 <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource" ref="dataSource" /> </bean> -->
<!-- 使用声明式事务 <tx:annotation-driventransaction-manager="txManager" /> -->
</beans>
3、数据源参数文件db.properties
···
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/scott?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
···
4、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>
<typeAlias type="com.neusoft.bean.Student" alias="Student"/>
</typeAliases>
</configuration>
5、StudentMapper接口
public interface StudentMapper {
public void save(Student student);
public Student getStudent(String id);
}
6、StudentMapper.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.neusoft.dao.StudentMapper">
<insert id="save" parameterType="Student">
insert intot_app_student(id,name,sex,age) values(#{id},#{name},#{sex},#{age})
</insert>
<select id="getStudent" resultType="Student" parameterType="String">
select * from t_app_student where id =#{id}
</select>
</mapper>
7、StudentService接口
public interface StudentService {
void save(Student student);
Student getStudent(String id);
}
8、StudentServiceImpl
@Service("studentService")
public class StudentServiceImpl implements StudentService{
@Autowired
private StudentMapper studentMapper;
public void save(Student student) {
studentMapper.save(student);
}
public Student getStudent(String id) {
Student student =studentMapper.getStudent(id);
return student;
}
}
9、Student实体类
public class Student {
private String id;
private String name;
private String sex;
private int age;
public Student(){
}
public Student(String id, String name, String sex,int age) {
this.id =id;
this.name =name;
this.sex =sex;
this.age =age;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex =sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age =age;
}
@Override
public String toString() {
return"Student [id=" +id +", name=" +name +", sex=" +sex
+ ", age=" + age + "]";
}