发表于: 2022-05-16 18:49:24
1 557
遇到的问题:Cannot resolve symbol 'ApplicationContext
解决打方法:pom中缺少依赖,导入依赖问题解决:
<!--spring框架 整合junit -->
<dependencies>
<!--spring框架 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.0-M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!--spring框架 整合junit -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!--spring框架 aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcTest {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
问题2:通配符的匹配很全面, 但无法找到元素 ‘context:annotation-config’ 的声明
解决方法:虽然定义了context的namespace但是没有指定context对应的xsd文件的位置。即需要在schemaLocation当中添加context对应的位置。
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
代码执行成功:
public class JdbcTest {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("jdbcTemplate.xml");
JdbcTemplateTest BiageService = context.getBean("BiageService",JdbcTemplateTest.class);
Biage b1 = new Biage();
b1.setName("小明");
b1.setType("程序员");
b1.setSchool("修真院");
BiageService.add(b1);
}
}
学习mybatis的创建:
pom中添加配置依赖:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
mysql当中创建表并添加字段:
创建配置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=D:\\java\\log\\mybatis-test\\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
创建配置db.properties文件:
db.driver=com.mysql.jdbc.Driver
#数据库连接字符串(改成自己的连接)
db.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&autoReconnect=true&allowMutiQueries=true&serverTimezone=Asia/Shanghai"
#数据库用户名(这里改成自己的用户名)
db.username=root
#数据库密码(这里改成自己的密码)
db.password=adbrk
明天计划:完成mybatis
评论