感觉写了好多,还没到正题....下一篇应该就可以到Elastic-Job了。
一、引入jar包:
com.h2database
h2
1.4.196
com.alibaba
druid
1.1.3
org.aspectj
aspectjweaver
1.9.0.BETA-6
pom
org.springframework
spring-jdbc
4.2.4.RELEASE
org.mybatis
mybatis
3.2.8
org.mybatis
mybatis-spring
1.2.2
org.aspectj
aspectjweaver
1.8.5
org.projectlombok
lombok
1.16.18
二、增加数据源配置:
1.新建目录
首先在/src/main/resources目录下新建目录:properties、spring-database(用于存放数据源的相关配置)、sql(用于存放初始化脚本)。
2.applicationContext.xml文件中增加配置:
3.数据源配置
/src/main/resources/spring-database增加数据源的配置文件application-db.xml。
4.事务配置
application-db.xml中增加事务配置。
5.集成MyBatis
我工程的Mybatis的配置文件是放在com.spring.lw.dao包下面。
步骤3、4、5操作完,application-db.xml文件的全部配置:
6.数据源参数配置
/src/main/resources/properties下面增加dbconfig.properties,我们使用的是内存数据库:
driverClassName=org.h2.Driver
validationQuery=SELECT 1
jdbc:h2:mem:DavisDB;MODE=MySql;DB_CLOSE_DELAY=-1
jdbc_username=sa
jdbc_password=sa
三.编写代码
1.增加VO:
package com.spring.lw.vo;
//不用写get/set方法了,回头介绍这个
import lombok.Data;
@Data
public class User {
private int id;
private String username;
private String password;
public String toString(){
return "id = " + id + " , username = " + username + ", password = " + password ;
}
}
2.编写DAO的接口和实现类、Service的接口和实现类
Dao接口:
package com.spring.lw.dao;
import com.spring.lw.vo.User;
public interface IUserDao {
User getUser(int id);
int insertUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
Dao实现类:
package com.spring.lw.dao.impl;
import com.spring.lw.dao.IUserDao;
import com.spring.lw.vo.User;
public class UserDaoImpl implements IUserDao {
@Override
public User getUser(int id) {
// TODO Auto-generated method stub
return null;
}
@Override
public int insertUser(User user) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int updateUser(User user) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int deleteUser(int id) {
// TODO Auto-generated method stub
return 0;
}
}
Service接口:
package com.spring.lw.service;
import com.spring.lw.vo.User;
public interface IUserService {
User getUser(int id);
int insertUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
Servie实现类:
package com.spring.lw.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spring.lw.dao.IUserDao;
import com.spring.lw.service.IUserService;
import com.spring.lw.vo.User;
@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public User getUser(int id) {
// TODO Auto-generated method stub
return userDao.getUser(id);
}
@Override
public int insertUser(User user) {
// TODO Auto-generated method stub
return userDao.insertUser(user);
}
@Override
public int updateUser(User user) {
// TODO Auto-generated method stub
return userDao.updateUser(user);
}
@Override
public int deleteUser(int id) {
// TODO Auto-generated method stub
return userDao.deleteUser(id);
}
}
3.Controller
新的接口路径,http://localhost:8088/Davis/getUser/{userId}
package com.spring.lw.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spring.lw.service.IUserService;
import com.spring.lw.vo.User;
@Controller
public class RestAPIController {
@Autowired
private IUserService userService ;
@RequestMapping("/hello")
public @ResponseBody String test() {
return "Spring MVC...";
}
@RequestMapping("/getUser/{userId}")
public @ResponseBody String getUser(@PathVariable String userId){
User user = userService.getUser(Integer.parseInt(userId));
return user == null ? "Can't find..." : user.toString();
}
}
4.MyBatis配置文件:
insert into user(id, username, password)
values(#{id,jdbcType=INTEGER},#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR});
update user
username=#{username,jdbcType=VARCHAR},
password=#{password,jdbcType=VARCHAR},
where id=#{id,jdbcType=INTEGER}
delete from user where id=#{id}
四.集成H2
1.在/src/main/resources/sql下面新建初始化脚本:
schema.sql
CREATE TABLE USER (ID int(11) NOT NULL , USERNAME varchar(20) , PASSWORD varchar(10));
data.sql
INSERT INTO USER (ID , USERNAME , PASSWORD) VALUES ('1', 'admin', 'admin');
INSERT INTO USER (ID , USERNAME , PASSWORD) VALUES ('2', 'zhangsan', '12345');
INSERT INTO USER (ID , USERNAME , PASSWORD) VALUES ('3', 'lisi', '11111');
INSERT INTO USER (ID , USERNAME , PASSWORD) VALUES ('4', 'wangwu', '22222');
INSERT INTO USER (ID , USERNAME , PASSWORD) VALUES ('5', 'zhaoliu', '33333');
2.在application-db.xml中增加配置,让工程启动的时候执行初始化脚本。
如果Eclips提示xml错误的话,增加:
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
上面完整的配置文件已经包含。
3.启动工程,在浏览器里面输入:
http://10.136.19.170:8088/Davis/getUser/1
可以查询到User的信息
http://10.136.19.170:8088/Davis/getUser/1
Can't find...