}
@Override
public String toString() {
return “Account{” +
“id=” + id +
“, name='” + name + ‘’’ +
“, money=” + money +
‘}’;
}
}
5. 编写Mapper接口
package com.itheima.mapper;
import com.itheima.domain.Account;
import java.util.List;
public interface AccountMapper {
// 保存账户信息
public void save(Account account);
// 查询账户信息
public List findAll();
}
6. 编写Service接口
package com.itheima.service;
import com.itheima.domain.Account;
import java.io.IOException;
import java.util.List;
public interface AccountService {
// 保存账户数据
public void save(Account account) throws IOException;
// 查询账户数据
public List findAll();
}
7. 编写Service接口实现
@Service(“accountService”)
public class AccountServiceImpl implements AccountService {
public void save(Account account) {
SqlSession sqlSession = MyBatisUtils.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
accountMapper.save(account);
sqlSession.commit();
sqlSession.close();
}
public List findAll() {
SqlSession sqlSession = MyBatisUtils.openSession();
AccountMapper accountMapper = sqlSession.getMapper(AccountMapper.class);
return accountMapper.findAll();
}
}
8. 编写Controller
@Controller
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(“/save”)
@ResponseBody
public String save(Account account){
accountService.save(account);
return “save success”;
}
@RequestMapping(“/findAll”)
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName(“accountList”);
modelAndView.addObject(“accountList”,accountService.findAll());
return modelAndView;
}
}
9. 编写添加页面
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
保存账户信息表单
用户名称
账户金额
10. 编写列表页面
<c:forEach items=“${accountList}” var=“account”>
${account.id} ${account.name} ${account.money}</c:forEach>
11. 编写相应配置文件(文件参考目录:素材/配置文件)
-
Spring配置文件:
applicationContext.xml
-
SprngMVC配置文件:
spring-mvc.xml
-
MyBatis映射文件:
AccountMapper.xml
-
MyBatis核心文件:
sqlMapConfig.xml
-
数据库连接信息文件:
jdbc.properties
-
Web.xml文件:
web.xml
-
日志文件:
log4j.properties
- jdbc.properties:存储数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true & characterEncoding=UTF-8 & serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=131415
- log4j.properties:日志记录
direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
direct messages to file mylog.log
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c:/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
set log levels - for more verbose logging change ‘info’ to ‘debug’
log4j.rootLogger=debug, stdout
- web.xml
<web-app xmlns=“http://xmlns.jcp.org/xml/ns/javaee”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd”
version=“4.0”>
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
DispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
DispatcherServlet
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
CharacterEncodingFilter
/*
① Spring监听器:Spring与Web结合
② SpringMVC前端控制器:SpringMVC与Web结合
③ 乱码过滤器
- applicationContext.xml:Spring配置文件
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package=“com.itheima”>
<context:exclude-filter type=“annotation”
expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
<context:property-placeholder location=“classpath:jdbc.properties”/>
<tx:advice id=“txAdvice”>
tx:attributes
<tx:method name=“*”/>
</tx:attributes>
</tx:advice>
aop:config
<aop:advisor advice-ref=“txAdvice” pointcut=“execution(* com.itheima.service.impl..(…))”/>
</aop:config>
组件扫描,主要是扫描service和mapper。要排除掉controller,因为controller主要是springMVC来控制扫描。
- spring-mvc.xml:SprngMVC配置文件
<beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package=“com.itheima.controller”/>
mvc:annotation-driven/
mvc:default-servlet-handler/
① 组件扫描——扫描controller
② 配置MVC注解驱动
③ 配置内部资源视图解析器,设置资源前后缀
④ 开放静态资源访问权限
- AccountMapper.xml:MyBatis映射文件
insert into account
values (#{id}, #{name}, #{money})
select *
from account
- sqlMapConfig.xml:MyBatis核心文件
12. 测试添加账户
13. 测试账户列表
1.2 Spring整合MyBatis
1. 整合思路
2. 将SqlSessionFactory配置到Spring容器中
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。
这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。
请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!**
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
一次偶然,从朋友那里得到一份“java高分面试指南”,里面涵盖了25个分类的面试题以及详细的解析:JavaOOP、Java集合/泛型、Java中的IO与NIO、Java反射、Java序列化、Java注解、多线程&并发、JVM、Mysql、Redis、Memcached、MongoDB、Spring、Spring Boot、Spring Cloud、RabbitMQ、Dubbo 、MyBatis 、ZooKeeper 、数据结构、算法、Elasticsearch 、Kafka 、微服务、Linux。
这不,马上就要到招聘季了,很多朋友又开始准备“金三银四”的春招啦,那我想这份“java高分面试指南”应该起到不小的作用,所以今天想给大家分享一下。
[外链图片转存中…(img-y4lTwyBC-1713310807869)]
请注意:关于这份“java高分面试指南”,每一个方向专题(25个)的题目这里几乎都会列举,在不看答案的情况下,大家可以自行测试一下水平 且由于篇幅原因,这边无法展示所有完整的答案解析
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!