项目整合
要求:
查询所有账户余额
查询某个账户余额
充值 给指定账户充值
提现 从指定账户提现
转账
1 导jar包
pom.xml中的jar引入
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.14.RELEASEversion>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
创建db.properties
jdbc.jdbcUrl = jdbc:mysql://localhost:3306/mybaits
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.user = root
jdbc.password = 123456
spring中applicationContext.xml配置文件的内容
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.lanou.pojo,com.lanou.dao,com.lanou.service,com.lanou.interceptor,com.lanou.utils">context:component-scan>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
<context:property-placeholder location="classpath:db.properties">context:property-placeholder>
<bean name="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
p:jdbcUrl="${jdbc.jdbcUrl}"
p:driverClass="${jdbc.driverClass}"
p:user="${jdbc.user}"
p:password="${jdbc.password}">
bean>
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
beans>
创建springmvc中的配置文件springmvc-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="com.lanou.controller">context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8value>
<value>application/json;charset=utf-8value>
<value>text/plain;charset=utf-8value>
<value>application/xml;charset=utf-8value>
list>
property>
bean>
mvc:message-converters>
mvc:annotation-driven>
<mvc:resources mapping="/static/**" location="/static/">mvc:resources>
beans>
3 创建好对应的包结构
在pojo包中创建与数据库表对应的ArAccount.java
public class ArAccount {
private Integer id;
private String username;
private Double money;
//...
}
在static 下创建js包引入jquey-xx-xx.js
创建一个静态页面
import com.alibaba.fastjson.JSON;
import com.lanou.pojo.ArAccount;
import com.lanou.service.ArAccountService;
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 java.util.List;
@Controller
@RequestMapping("/arAccount")
public class ArAcountController {
@Autowired
private ArAccountService arAccountService;
@RequestMapping("/findAll")
@ResponseBody
public String fincdAll(){
List<ArAccount> list = arAccountService.findAll();
return JSON.toJSONString(list);
}
}
ArAccountService 接口
public interface ArAccountService {
//查询所有账户余额
List<ArAccount> findAll();
}
实现类
@Service
public class ArAccountServiceImpl implements ArAccountService {
@Autowired
private ArAccountDao dao;
@Override
public List<ArAccount> findAll() {
return dao.findAll();
}
ArAccountDao接口
public interface ArAccountDao {
//查询所有账户余额
List<ArAccount> findAll();
}
ArAccountDaoImpl实现类
@Repository
public class ArAccountDaoImpl implements ArAccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public List<ArAccount> findAll() {
String sql = "select * from ar_account";
List<ArAccount> list = jdbcTemplate.query(sql, new RowMapper<ArAccount>() {
@Override
public ArAccount mapRow(ResultSet resultSet, int i) throws SQLException {
ArAccount arAccount = new ArAccount();
arAccount.setId(resultSet.getInt("id"));
arAccount.setUsername(resultSet.getString("username"));
arAccount.setMoney(resultSet.getDouble("money"));
return arAccount;
}
});
return list;
}
<script type="text/javascript" src="/static/js/jquery-3.4.1.js">script>
<script type="text/javascript">
$(function () {
$.get("/arAccount/findAll",function (data) {
for (var s=0; s< data.length;s++){
$("tbody").append(""+data[s].id+" "+data[s].username+" "+data[s].money+" ")
}
},"json");
});
script>
前端页面
<table>
<thead>
<tr>
<td>idtd>
<td>账户名td>
<td>资金td>
tr>
thead>
<tbody>
tbody>
table>