maven+springMVC+mybatis+junit详细搭建过程(二)

3. 配置数据库连接属性

conf/ jdbc.properties(jdbc配置文件)

1 jdbc_driverClassName=com.mysql.jdbc.Driver
2 jdbc_url=jdbc:mysql://localhost:3306/mydays?useUnicode=true&characterEncoding=utf-8
3 jdbc_username=root
4 jdbc_password=root

4.  配置spring配置文件

    conf/spring.xml(spring配置文件的扫描)


01 <?xml version="1.0" encoding="UTF-8"?>
02 <beans xmlns="http://www.springframework.org/schema/beans"
03  xmlns:context="http://www.springframework.org/schema/context"
04  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05  xsi:schemaLocation="http://www.springframework.org/schema/beans
06       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
07       http://www.springframework.org/schema/context
08       http://www.springframework.org/schema/context/spring-context.xsd">
09  
10     <!-- 引入jdbc配置文件 -->
11     <context:property-placeholder location="classpath:conf/jdbc.properties"/>
12      
13     <!-- 扫描文件(自动将servicec层注入) -->
14     <context:component-scan base-package="cn.springmvc.service"/>
15 </beans>
  conf/spring-mybatis.xml(spring与mybatis连接属性)
01 <?xml version="1.0" encoding="UTF-8"?>
02 <beans xmlns="http://www.springframework.org/schema/beans"
03   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04   xmlns:p="http://www.springframework.org/schema/p"
05   xmlns:context="http://www.springframework.org/schema/context"
06   xmlns:aop="http://www.springframework.org/schema/aop"
07   xmlns:tx="http://www.springframework.org/schema/tx"
08   xmlns:util="http://www.springframework.org/schema/util"
09   xsi:schemaLocation="http://www.springframework.org/schema/beans
10     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
11     http://www.springframework.org/schema/context
12     http://www.springframework.org/schema/context/spring-context-3.2.xsd
13     http://www.springframework.org/schema/tx
14     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
15     http://www.springframework.org/schema/aop
16     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
17     http://www.springframework.org/schema/util
18     http://www.springframework.org/schema/util/spring-util-3.2.xsd">
19  
20     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init"
21     destroy-method="close" >
22     <property name="driverClassName">
23       <value>${jdbc_driverClassName}</value>
24     </property>
25     <property name="url">
26       <value>${jdbc_url}</value>
27     </property>
28     <property name="username">
29       <value>${jdbc_username}</value>
30     </property>
31     <property name="password">
32       <value>${jdbc_password}</value>
33     </property>
34     <!-- 连接池最大使用连接数 -->
35     <property name="maxActive">
36       <value>20</value>
37     </property>
38     <!-- 初始化连接大小 -->
39     <property name="initialSize">
40       <value>1</value>
41     </property>
42     <!-- 获取连接最大等待时间 -->
43     <property name="maxWait">
44       <value>60000</value>
45     </property>
46     <!-- 连接池最大空闲 -->
47     <property name="maxIdle">
48       <value>20</value>
49     </property>
50     <!-- 连接池最小空闲 -->
51     <property name="minIdle">
52       <value>3</value>
53     </property>
54     <!-- 自动清除无用连接 -->
55     <property name="removeAbandoned">
56       <value>true</value>
57     </property>
58     <!-- 清除无用连接的等待时间 -->
59     <property name="removeAbandonedTimeout">
60       <value>180</value>
61     </property>
62     <!-- 连接属性 -->
63     <property name="connectionProperties">
64       <value>clientEncoding=UTF-8</value>
65     </property>
66   </bean>
67      
68     <!-- mybatis文件配置,扫描所有mapper文件 -->
69       <bean id="sqlSessionFactory"
70           class="org.mybatis.spring.SqlSessionFactoryBean"
71           p:dataSource-ref="dataSource"
72           p:configLocation="classpath:conf/mybatis-config.xml"
73           p:mapperLocations="classpath:mapper/*.xml"/><!-- configLocation为mybatis属性 mapperLocations为所有mapper-->
74        
75    <!-- spring与mybatis整合配置,扫描所有dao -->
76  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
77         p:basePackage="cn.springmvc.dao"
78         p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
79   
80    <!-- 对数据源进行事务管理 -->
81   <bean id="transactionManager"
82         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
83         p:dataSource-ref="dataSource"/>
84 </beans>

5.  java代码编写(model,dao,service层代码)

   cn.springmvc.model/User.java(用户基本信息)


01 package cn.springmvc.model;
02  
03  
04 /**
05  * 用户表
06  */
07 public class User {
08  
09     private int id;
10     private int state;
11     private String nickname;
12     public int getId() {
13         return id;
14     }
15     public void setId(int id) {
16         this.id = id;
17     }
18     public int getState() {
19         return state;
20     }
21     public void setState(int state) {
22         this.state = state;
23     }
24     public String getNickname() {
25         return nickname;
26     }
27     public void setNickname(String nickname) {
28         this.nickname = nickname;
29     }
30 }



cn.springmvc.dao/UserDAO.java(dao操作接口)



01 package cn.springmvc.dao;
02  
03 import cn.springmvc.model.User;
04  
05  
06 public interface UserDAO {
07  
08     /**
09      * 添加新用户
10      * @param user
11      * @return
12      */
13     public int insertUser(User user);
14      
15      
16 }

cn.springmvc.service/UserService.java(service层接口)


1 package cn.springmvc.service;
2  
3 import cn.springmvc.model.User;
4  
5  
6 public interface UserService {
7  
8     public int insertUser(User user);
9 }



cn.springmvc.service.impl/UserServiceImpl.java(service层接口实现)




01 package cn.springmvc.service.impl;
02  
03 import org.springframework.beans.factory.annotation.Autowired;
04 import org.springframework.stereotype.Service;
05  
06 import cn.springmvc.dao.UserDAO;
07 import cn.springmvc.model.User;
08 import cn.springmvc.service.UserService;
09  
10  
11 @Service
12 public class UserServiceImpl implements UserService{
13  
14     @Autowired
15     private UserDAO userDAO;
16      
17     @Override
18     public int insertUser(User user) {
19         // TODO Auto-generated method stub
20         return userDAO.insertUser(user);
21     }
22  
23 }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值