mybatis 整合dao 层代码;
需要配置 mybatis-config.xml 用于使用spring 实例化一个sqlsessionFactoryBean 工厂类。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 使用properties 文件 -->
<context:property-placeholder location="classpath:db/db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mysql.jdbc.driver}"></property>
<property name="jdbcUrl" value="${mysql.jdbc.url}"></property>
<property name="user" value="${mysql.jdbc.userName}"></property>
<property name="password" value="${mysql.jdbc.passWord}"></property>
<!-- 当连接池中连接耗尽时,一次获取值 -->
<property name="acquireIncrement" value="${mysql.jdbc.acquireIncrement}"></property>
<property name="initialPoolSize" value="${mysql.jdbc.initialSize}"></property>
<property name="minPoolSize" value="${mysql.minPoolSize}"></property>
<property name="maxPoolSize" value="${mysql.maxPoolSize}"></property>
</bean>
<!-- 使用会话配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:mappings/*.xml"></property>
</bean>
<!-- 扫描mapper 接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fandong.it.weapon.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事物 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用注解事务 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
</beans>
mappings 文件下是使用xml 进行sql 实例化对象
<?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.fandong.it.weapon.mapper.UserMapper">
<select id="getUser" parameterType="int" resultType="com.fandong.it.weapon.entity.User" >
select * from user where age=#{age}
</select>
</mapper>
db文件中是 mysql数据的配置
mysql.jdbc.url=jdbc:mysql://hadoop03.fandong.com:3306/springStudy
mysql.jdbc.driver=com.mysql.jdbc.Driver
mysql.jdbc.userName=nodejs1
mysql.jdbc.passWord=nodejs1
mysql.jdbc.acquireIncrement=5
mysql.jdbc.initialSize=0
mysql.jdbc.maxActive=20
mysql.minPoolSize=5
mysql.maxPoolSize=20
--是一个实体类
/**
*
*/
package com.fandong.it.weapon.entity;
/**
* @author fandong
*
*/
public class User {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
private String name;
private Integer age;
}
--mapper 接口方法
/**
*
*/
package com.fandong.it.weapon.mapper;
import com.fandong.it.weapon.entity.User;
/**
* @author fandong
*
*/
public interface UserMapper {
User getUser(int id);
}
在impl module 中引用
这样其实就是使用jdbcTemplate 添加了一个dao层,进行控制数据,把这里面的sql 进行抽离出去。
在Controller 中调用service 方法
启动发现报错,无法tx 注解
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [mybatis-config.xml]
解决方案: 这个是tx 事务的无法解析引入 spring-jdbc 即可。
2 、
一月 10, 2020 6:19:05 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
严重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [mybatis-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [org.mybatis.spring.SqlSessionFactoryBean]: Bean property 'datasource' is not writable or has an invalid setter method. Did you mean 'dataSource'?
解决方案:<property name="dataSource" ref="dataSource"></property> name 的 datasource 小写了,改成大写即可。
3、
严重: Servlet.service() for servlet [springmvc] in context with path [/mybatisWeb] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.fandong.it.weapon.mapper.UserMapper.getUser]
分析 mapper
一定要指定到类
<mapper namespace="com.fandong.it.weapon.mapper.UserMapper">
4、启动OK,数据库中
browser中
至此项目搭建完成。