spring和mybaits整合
来源:互联网 发布:软件人才培训班 编辑:程序博客网 时间:2024/05/02 01:50
spring + mybatis + mysql/oracle开发
- 创建一个spring-mybatis-mysql这么一个javaweb或java工程
- 导入spring-ioc,spring-aop,spring-transaction,mybatis,c3p0,mysql/oracle相关的jar包和spring整合mybatis的jar包
- 创建students.sql
--mysqlcreate table students( sid int(5) primary key, sname varchar(10), ssal double(8,2));
- 创建Student.java
/** * 学生 * @author csx */public class Student {private Integer id;//编号private String name;//姓名private Double sal;//薪水public Student(){}public Student(Integer id, String name, Double sal) {this.id = id;this.name = name;this.sal = sal;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Double getSal() {return sal;}public void setSal(Double sal) {this.sal = sal;}}
- 创建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="studentNamespace"><resultMap type="cn.csx.javaee.mybatis.entity.Student" id="studentMap"><id property="id" column="sid" /><result property="name" column="sname"/><result property="sal" column="ssal"/></resultMap><insert id="insert" parameterType="cn.csx.javaee.mybatis.entity.Student">insert into students(sid,sname,ssal) values(#{id},#{name},#{sal})</insert></mapper>
- 创建StudentDao.java
public class StudentDao {private SqlSessionFactory sqlSessionFactory;public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {this.sqlSessionFactory = sqlSessionFactory;}public void insert(Student student){SqlSession sqlSession = sqlSessionFactory.openSession();sqlSession.insert("studentNamespace.insert",student);//int i = 10/0;}public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml"});StudentDao studentDao = (StudentDao) ac.getBean("studentDaoID");studentDao.insert(new Student(1,"哈哈",7000D));}}
- 在src目录下创建mybatis.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><mappers><mapper resource="cn/csx/javaee/mybatis/entity/StudentMapper.xml"/></mappers></configuration>
- 在src目录下创建spring.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 配置C3P0连接池(即管理数据库连接) --> <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置SqlSessionFactoryBean(即替代MyBatisUtil工具类的作用) --> <bean id="sqlSessionFactoryBeanID" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="comboPooledDataSourceID"/> <property name="configLocation" value="classpath:mybatis.xml"/> </bean> <!-- 配置事务管理器(即使用JDBC事务管理器) --> <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="comboPooledDataSourceID"/> </bean> <!-- 配置事务通知(即哪些方法需要事务) --> <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务切面(即哪些包中的类需要事务通知) --> <aop:config> <aop:pointcut id="pointcut" expression="execution(* cn.itcast.javaee.mybatis.dao.*.*(..))"/> <aop:advisor advice-ref="tx" pointcut-ref="pointcut" /> </aop:config> <!-- 配置StudentDao类 --> <bean id="studentDaoID" class="cn.itcast.javaee.mybatis.dao.StudentDao"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBeanID"/> </bean> </beans>
1 0
- Spring 和 mybaits 整合
- spring和mybaits整合
- spring-boot-mybaits 整合
- springmvc和mybaits整合案例
- Spring 学习日记 (四) Spring 整合Mybaits 和 struts2 框架的配置文件
- Eclipse Meaven Spring SpringMVC Mybaits整合
- Spring+SpringMVC+MyBaits+Druid+Maven项目整合
- spring boot 整合 mybaits之XML
- Spring Boot教程五:整合mybaits
- 整合spring和myBaits时候报这个错是怎么回事?跪求解答
- mybaits 整合
- Spring整合Mybaits实现ehcache 注解查询缓存
- Spring-mybaits-mina整合,启动多端口监听
- spring-boot系列——与mybaits整合
- Spring整合Mybaits实现ehcache 注解查询缓存
- springmvc和mybaits整合(五)-商品查询controller
- redis-cluster整合mybaits
- springmvc+mybaits整合
- JAVA WEB 实现分页技术
- ffmpeg软解h264速度与ref数的关系
- Hibernate一级缓存与二级缓存
- 几种常见的Shell
- 迭代器失效小分析
- spring和mybaits整合
- div可拉伸插件
- FFMpeg在Windows下搭建开发环境
- "围观"设计模式(23)--行为型之命令模式(Command Pattern)
- Shell脚本语言与编译型语言的差异
- 值得学习的Unity工程或插件项目
- 111. Minimum Depth of Binary Tree 二叉树的最小深度
- Summary Ranges
- iOS 截屏 长图 拼图