3. 配置数据库连接属性
conf/ jdbc.properties(jdbc配置文件)
1 | jdbc_driverClassName=com.mysql.jdbc.Driver |
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"> |
11 | < context:property-placeholder location = "classpath:conf/jdbc.properties" /> |
14 | < context:component-scan base-package = "cn.springmvc.service" /> |
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"> |
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 > |
26 | < value >${jdbc_url}</ value > |
28 | < property name = "username" > |
29 | < value >${jdbc_username}</ value > |
31 | < property name = "password" > |
32 | < value >${jdbc_password}</ value > |
35 | < property name = "maxActive" > |
39 | < property name = "initialSize" > |
43 | < property name = "maxWait" > |
47 | < property name = "maxIdle" > |
51 | < property name = "minIdle" > |
55 | < property name = "removeAbandoned" > |
59 | < property name = "removeAbandonedTimeout" > |
63 | < property name = "connectionProperties" > |
64 | < value >clientEncoding=UTF-8</ value > |
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" /> |
76 | < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" |
77 | p:basePackage = "cn.springmvc.dao" |
78 | p:sqlSessionFactoryBeanName = "sqlSessionFactory" /> |
81 | < bean id = "transactionManager" |
82 | class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" |
83 | p:dataSource-ref = "dataSource" /> |
5. java代码编写(model,dao,service层代码)
cn.springmvc.model/User.java(用户基本信息)
01 | package cn.springmvc.model; |
11 | private String nickname; |
15 | public void setId( int id) { |
18 | public int getState() { |
21 | public void setState( int state) { |
24 | public String getNickname() { |
27 | public void setNickname(String nickname) { |
28 | this .nickname = nickname; |
cn.springmvc.dao/UserDAO.java(dao操作接口)
01 | package cn.springmvc.dao; |
03 | import cn.springmvc.model.User; |
06 | public interface UserDAO { |
13 | public int insertUser(User user); |
cn.springmvc.service/UserService.java(service层接口)
1 | package cn.springmvc.service; |
3 | import cn.springmvc.model.User; |
6 | public interface UserService { |
8 | public int insertUser(User user); |
cn.springmvc.service.impl/UserServiceImpl.java(service层接口实现)
01 | package cn.springmvc.service.impl; |
03 | import org.springframework.beans.factory.annotation.Autowired; |
04 | import org.springframework.stereotype.Service; |
06 | import cn.springmvc.dao.UserDAO; |
07 | import cn.springmvc.model.User; |
08 | import cn.springmvc.service.UserService; |
12 | public class UserServiceImpl implements UserService{ |
15 | private UserDAO userDAO; |
18 | public int insertUser(User user) { |
20 | return userDAO.insertUser(user); |