ssm+shiro框架搭建笔记(3)

来源:互联网 发布:xbox网络teredo不合格 编辑:程序博客网 时间:2024/05/16 17:09

配置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.DrivervalidationQuery=SELECT 1jdbc_url=jdbc:mysql://localhost:3306/maven-ssm?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNulljdbc_username=longjdbc_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;@Controllerpublic class LoginController {@Autowiredprivate LoginService loginService;@RequestMapping(value="/login")@ResponseBodypublic 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>
0 0
原创粉丝点击