ssm+shiro框架搭建笔记(3)
2017-03-02 10:47
260 查看
配置mybatis
(1). 配置Spring.xml。文件添加一下配置信息:
<!-- 数据库连接配置 --> <import resource="spring-mybatis.xml"/>
(2).添加配置spring-mybatis.xml文件。
文件配置信息:
<!-- 引入属性文件 --> <context:property-placeholder location="classpath:config.properties" /> <!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="0" /> <!-- 连接池最大使用连接数量 --> <property name="maxActive" value="20" /> <!-- 连接池最大空闲 --> <property name="maxIdle" value="20" /> <!-- 连接池最小空闲 --> <property name="minIdle" value="0" /> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000" /> <!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> --> <property name="validationQuery" value="${validationQuery}" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="true" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="true" /> <!-- 监控数据库 --> <!-- <property name="filters" value="stat" /> --> <property name="filters" value="mergeStat" /> </bean> <!-- myBatis文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 --> <property name="mapperLocations" value="classpath:com/lcl/*/mapping/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.lcl.*.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
(3).引入config.properties属性文件。
文件内容如下:
driverClassName=com.mysql.jdbc.Driver validationQuery=SELECT 1 jdbc_url=jdbc:mysql://localhost:3306/maven-ssm?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc_username=long jdbc_password=long
(4).测试代码。
LoginController.java—Controller类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.lcl.sys.service.LoginService; @Controller public class LoginController { @Autowired private LoginService loginService; @RequestMapping(value="/login") @ResponseBody public List<Test> doLogin(){ //String str = loginService.Login("login"); List<Test> list = loginService.selectTestAll(); for(Test obj:list){ System.out.println(obj.toString()); } return list; } }
LoginService.java—Service接口
public interface LoginService { public String Login(String info); public List<Test> selectTestAll(); }
LoginServiceImp—Service接口实现类
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.lcl.sys.dao.TestMapper; import com.lcl.sys.model.Test; import com.lcl.sys.service.LoginService; @Service(value="loginService") public class LoginServiceImp implements LoginService { @Autowired private TestMapper testMapper; public String Login(String info) { return info; } public List<Test> selectTestAll() { return testMapper.selectTestAll(); } }
TestMapper.java—dao接口
import java.util.List; import com.lcl.sys.model.Test; public interface TestMapper { List<Test> selectTestAll(); }
Test.java—实体类
public class Test { private Integer id; private String author; private String text; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author == null ? null : author.trim(); } public String getText() { return text; } public void setText(String text) { this.text = text == null ? null : text.trim(); } @Override public String toString() { return "[Id:"+this.getId()+"Author:"+this.getAuthor()+"Text:"+this.getText()+"]"; } }
TestMapper.xml—sql配置文件
<mapper namespace="com.lcl.sys.dao.TestMapper" > <resultMap id="TestBaseResultMap" type="com.lcl.sys.model.Test" > <id column="Id" property="id" jdbcType="INTEGER" /> <result column="author" property="author" jdbcType="VARCHAR" /> <result column="text" property="text" jdbcType="VARCHAR" /> </resultMap> <select id="selectTestAll" resultMap="TestBaseResultMap" > select Id, author, text from test </select> </mapper>
相关文章推荐
- ssm+shiro框架搭建笔记(4)
- ssm+shiro框架搭建笔记(1)
- ssm+shiro框架搭建笔记(7)
- ssm+shiro框架搭建笔记(5)
- ssm+shiro框架搭建笔记(2)
- JavaWeb学习笔记之SSM框架搭建配置文件
- ssm+shiro基础框架搭建(一)
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置
- SSM框架的搭建(个人笔记)
- ssm环境下快速搭建shiro框架
- SpringMVC笔记——SSM框架搭建简单实例
- ssm搭建shiro安全框架
- SSM框架搭建项目整合学习笔记
- 入这行三个月后写的第一篇笔记,使用maven搭建ssm的框架
- J2EE系列之MyBatis学习笔记(十三)-- SSM框架搭建
- ssm+shiro基础框架搭建(二)
- ssm框架整合笔记-1-基础框架搭建
- SpringMVC笔记——SSM框架搭建简单实例
- SpringMVC笔记——SSM框架搭建简单实例
- SpringMVC笔记——SSM框架搭建简单实例