第三步:导入log4j.properties配置文件
Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE
Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
第四步:测试 spring 能否独立运行
测试代码:
package com.keafmd.test;
import com.keafmd.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
Keafmd
-
@ClassName: TestSpring
-
@Description: 测试类
-
@author: 牛哄哄的柯南
-
@date: 2021-02-17 22:37
*/
public class TestSpring {
@Test
public void run1(){
//加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”);
//获取对象
AccountService as = (AccountService)ac.getBean(“accountService”);
//调用方法
as.findAll();
}
}
运行结果:
业务层,查询所有。。。
Process finished with exit code 0
第一步:在 web.xml 中配置核心控制器(DispatcherServlet)
web.xml:
Archetype Created Web Application
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
第二步:编写 SpringMVC 的配置文件
springmvc.xml:
<?xml version="1.0" encoding="UTF-8"?><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.keafmd”>
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
<bean id=“viewResolver”
class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>
<mvc:resources location=“/css/” mapping=“/css/**”/>
<mvc:resources location=“/images/” mapping=“/images/**”/>
<mvc:resources location=“/js/” mapping=“/js/**”/>
mvc:annotation-driven/
第三步:编写 Controller 和 jsp 页面
index.jsp:
<%–
Created by IntelliJ IDEA.
User: Keafmd
Date: 2021/2/17
Time: 23:20
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
AccountController:
package com.keafmd.controller;
import com.keafmd.domain.Account;
import com.keafmd.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
-
Keafmd
-
@ClassName: AccountController
-
@Description: 账户控制器 web
-
@author: 牛哄哄的柯南
-
@date: 2021-02-17 21:19
*/
@Controller
@RequestMapping(“/account”)
public class AccountController {
/**
-
查询所有
-
@param
-
@return
*/
@RequestMapping(“/findAll”)
public String findAll(){
System.out.println(“表现层,查询所有账户信息。。。”);
return “list”;
}
}
list.jsp:
<%–
Created by IntelliJ IDEA.
User: Keafmd
Date: 2021/2/18
Time: 15:50
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
查询所有的账户
第四步:部署tomcat服务器
启动服务器,进行测试
控制台输出:
表现层,查询所有账户信息。。。
目的:在controller中能成功的调用service对象中的方法。
第一步:配置监听器实现启动服务创建容器
在web.xml添加监听器:
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
web.xml:
Archetype Created Web Application
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
第二步:在controller中注入service对象,调用service对象的方法进行测试
package com.keafmd.controller;
import com.keafmd.domain.Account;
import com.keafmd.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
-
Keafmd
-
@ClassName: AccountController
-
@Description: 账户控制器 web
-
@author: 牛哄哄的柯南
-
@date: 2021-02-17 21:19
*/
@Controller
@RequestMapping(“/account”)
public class AccountController {
@Autowired
private AccountService accountService;
/**
-
查询所有
-
@return
*/
@RequestMapping(“/findAll”)
public String findAll(){
System.out.println(“表现层,查询所有账户信息。。。”);
//调用service的方法
accountService.findAll();
return “list”;
}
}
运行结果:
控制台输出:
表现层,查询所有账户信息。。。
业务层,查询所有。。。
第一步:在web项目中编写SqlMapConfig.xml的配置文件,编写核心配置文件
SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>jdbcConfig.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
第二步:在AccountDao接口的方法上添加注解,编写SQL语句
package com.keafmd.dao;
import com.keafmd.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
-
Keafmd
-
@ClassName: AccountDao
-
@Description: 账户dao接口
-
@author: 牛哄哄的柯南
-
@date: 2021-02-17 21:05
*/
public interface AccountDao {
//查询所有账户
@Select(“select * from account”)
public List findAll();
//保存账户
@Insert(“insert into account(name,money) values(#{name},#{money})”)
public void saveAccount(Account account);
}
第三步:编写测试的方法
TestMybatis:
package com.keafmd.test;
import com.keafmd.dao.AccountDao;
import com.keafmd.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
/**
-
Keafmd
-
@ClassName: TestMybatis
-
@Description: 测试Mybatis
-
@author: 牛哄哄的柯南
-
@date: 2021-02-18 16:37
*/
public class TestMybatis {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private AccountDao accountDao;
@Before
public void init() throws Exception{
//加载配置文件
in = Resources.getResourceAsStream(“SqlMapConfig.xml”);
//创建SqlSessionFactory对象
factory = new SqlSessionFactoryBuilder().build(in);
//创建SqlSession对象
session = factory.openSession();
//获取到代理对象
accountDao = session.getMapper(AccountDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
/**
- 查询所有
*/
@Test
public void run1(){
List accounts = accountDao.findAll();
for (Account account : accounts) {
System.out.println(account);
}
}
/**
- 测试保存
*/
@Test
public void saveAccount(){
Account account = new Account();
account.setName(“毛利小五郎”);
account.setMoney(800.00);
// account.setMoney(800d);
accountDao.saveAccount(account);
}
}
运行run1():
Account{id=1, name=‘keafmd’, money=9000.0}
Account{id=2, name=‘小兰’, money=8000.0}
Account{id=3, name=‘新一’, money=8000.0}
Account{id=4, name=‘毛利小五郎’, money=800.0}
Account{id=5, name=‘怪盗基德’, money=660.0}
Account{id=6, name=‘某某’, money=290.0}
Process finished with exit code 0
整合思路:把 mybatis 配置文件(SqlMapConfig.xml)中内容配置到 spring 配置文件中同时,把 mybatis 配置文件的内容清掉。
第一步:把SqlMapConfig.xml配置文件中的内容配置到applicationContext.xml配置文件中
把以下内容添加到applicationContext.xml配置文件中:
SqlMapConfig.xml和jdbcConfig.properties可以删除了。
第二步:在applicationContext.xml配置文件中配置 spring 的事务
把以下内容添加到applicationContext.xml配置文件中:
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true”/>
<tx:method name=“*” isolation=“DEFAULT”/>
</tx:attributes>
</tx:advice>
aop:config
<aop:pointcut expression=“execution(* com.keafmd.service.impl..(…))” id=“pt1”/>
<aop:advisor advice-ref=“txAdvice” pointcut-ref=“pt1”/>
</aop:config>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><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:tx=“http://www.springframework.org/schema/tx”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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">
<context:component-scan base-package=“com.keafmd”>
<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>
tx:attributes
<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true”/>
<tx:method name=“*” isolation=“DEFAULT”/>
</tx:attributes>
</tx:advice>
aop:config
<aop:pointcut expression=“execution(* com.keafmd.service.impl..(…))” id=“pt1”/>
<aop:advisor advice-ref=“txAdvice” pointcut-ref=“pt1”/>
</aop:config>
第三步:在AccountDao接口中添加@Repository注解
AccountDao:
package com.keafmd.dao;
import com.keafmd.domain.Account;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
-
Keafmd
-
@ClassName: AccountDao
-
@Description: 账户dao接口
-
@author: 牛哄哄的柯南
-
@date: 2021-02-17 21:05
*/
@Repository
public interface AccountDao {
//查询所有账户
@Select(“select * from account”)
public List findAll();
//保存账户
@Insert(“insert into account(name,money) values(#{name},#{money})”)
public void saveAccount(Account account);
}
第四步:在service中注入dao对象
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
最后
全网独播-价值千万金融项目前端架构实战
从两道网易面试题-分析JavaScript底层机制
RESTful架构在Nodejs下的最佳实践
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
一线互联网企业如何初始化项目-做一个自己的vue-cli
思维无价,看我用Nodejs实现MVC
代码优雅的秘诀-用观察者模式深度解耦模块
前端高级实战,如何封装属于自己的JS库
VUE组件库级组件封装-高复用弹窗组件
从两道网易面试题-分析JavaScript底层机制
RESTful架构在Nodejs下的最佳实践
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
一线互联网企业如何初始化项目-做一个自己的vue-cli
思维无价,看我用Nodejs实现MVC
代码优雅的秘诀-用观察者模式深度解耦模块
前端高级实战,如何封装属于自己的JS库
VUE组件库级组件封装-高复用弹窗组件