搭建Spring SpringMVC Mybatis 框架(二)
来源:互联网 发布:windows pyqt5 教程 编辑:程序博客网 时间:2024/05/16 11:29
上一章我们引入了相关的jar包
本章我们来进行配置
1 在src/main/resources 包中创建spring的配置文件 applicationContext.xml
2 在 src/main/java中创建相关的包
接下来 我们以student表为例 写一个测试
1 创建一个名叫 Student的javabean
2 在dao包中创建一个接口 定义一个读取数据的方法
public interface IstudentDao {public List<Student> getStudents();}3 在dao中定义mapper 实现该方法
<?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.wy.dao.IstudentDao"> <select id="getStudents" resultType="Student"> select * from student </select></mapper>4 在service中创建一个接口 定义一个读取数据的方法
public interface IstudentService {
public List<Student> getStudents();
}
5 在service中创建一个类 实现上面的接口
@Servicepublic class StudentServiceImpl implements IstudentService{@Resourceprivate IstudentDao studentDao;@Overridepublic List<Student> getStudents() {// TODO Auto-generated method stubreturn studentDao.getStudents();}}
打开applicationContext.xml 我们配置 扫描service包 数据库连接池 mybatis相关配置 事务管理等
<?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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><context:component-scan base-package="com.wy.service"></context:component-scan><!-- Hikari Datasource --> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="123456" /> <!-- 连接只读数据库时配置为true, 保证安全 --> <property name="readOnly" value="false" /> <!-- 等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒 --> <property name="connectionTimeout" value="30000" /> <!-- 一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟 --> <property name="idleTimeout" value="600000" /> <!-- 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';) --> <property name="maxLifetime" value="1800000" /> <!-- 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count) --> <property name="maximumPoolSize" value="15" /> </bean> <!-- Mybatis SqlSession工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 映射所有的sql对应的模块的xml文件 --> <property name="mapperLocations" value="classpath:com/wy/dao/*.xml"></property> <!-- 注册mybatisbean别名 --> <property name="typeAliasesPackage" value="com.wy.bean"></property> </bean> <!-- 扫mybatis代理对象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wy.dao"></property> </bean> <!-- 配置事务管理 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean> <tx:annotation-driven transaction-manager="txManager" /> </beans>
配置完成 我们来测试一下
@RunWith(value=SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext.xml")public class test {@Resourceprivate IstudentService studentService;@Testpublic void test(){List<Student> students=studentService.getStudents();for (Student student : students) {System.out.println(student);}}
}运行结果如下
我们看下数据库
0 0
- 搭建Spring SpringMVC Mybatis 框架(二)
- Maven+Spring+SpringMVC+MyBatis框架的搭建(二)
- 利用Maven搭建Spring+SpringMVC+Mybatis框架项目(二)
- SSM框架搭建(Spring+SpringMVC+MyBatis)
- 搭建Spring SpringMVC Mybatis 框架(一)
- SSM框架搭建(SpringMVC+Spring+Mybatis)
- springMVC+Spring+Mybatis框架搭建
- spring+springMVC+mybatis框架搭建
- Spring+SpringMVC+Mybatis框架搭建
- Spring+SpringMVC+MyBatis框架搭建
- springmvc+spring+mybatis框架搭建
- Spring+SpringMVC+Mybatis框架搭建
- 搭建Spring+SpringMVC+mybatis框架
- Spring+SpringMVC+Mybatis框架搭建
- springMVC+spring+Mybatis框架搭建
- 搭建SpringMVC + Spring + Mybatis 框架
- java开发SSM框架的搭建(SpringMVC+Spring+MyBatis)
- spring+springmvc+mybatis(mysql/sqlserver)框架搭建
- hiho-1015- KMP算法
- oop &&GP 模板 ---> 特化和偏特化
- 归并排序
- Docker容器: 那些你不知道的事
- 友元函数和static类
- 搭建Spring SpringMVC Mybatis 框架(二)
- 服务(service)-----本地服务
- Java 四大域对象简述
- Android7.0 Doze模式分析 Doze介绍 & DeviceIdleController
- 2017年最新版贵州房卡麻将源码| 网狐6603麻将、房卡类子游戏《贵阳捉鸡麻将》:带手机端+服务端(含内核通信源码)+网站后台,包含多种玩法,支持微信登陆
- 蓝桥杯ALGO-148(最小公倍数 )
- selenium之 时间日期控件的处理
- 学习PHP第一天HELLO WORLD
- 文章标题