发表于: 2021-11-19 22:20:42
1 1196
今天完成的事:用maven 搭建spring springmvc mybatis整合 jdk用的是 java8 maven版本3.8.3 tomcat 8.5.72
用maven 加载jar包pom.xml 文件 具体代码已上传至gitee 记录以后备用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ssmdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.org</include>
<include>**/*.txt</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.org</include>
<include>**/*.txt</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<finalName>ssmdemo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>8</target>
<source>8</source>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
SSM框架目录
连接数据库测试类的2种方法
方法1:
public class TestMybatis {
private ApplicationContext ac;
private StudentService studentService;
@Before
public void test(){
ac= new ClassPathXmlApplicationContext(new String[]{"spring-mybatis.xml"});
studentService=(StudentService) ac.getBean("stuService");
System.out.println("连接数据库成功!");
}
@Test
public void test1(){
Student s= studentService.getStuById(1);
System.out.println(s);
}
}
输出结果
连接数据库成功!
Student{sid='1'sname='邓科荣'cid='1'sex='男'birthplace='湖南'}
方法2:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestMybatisdemo {
public StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
@Autowired
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Test
public void MybatisDemoTest(){
System.out.println("===============>连接数据库成功");
Integer sid=1;
Student student=studentService.getStuById(sid);
System.out.println(student);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mybatis.xml"})
public class TestMybatisdemo {
public StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
@Autowired
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
@Test
public void MybatisDemoTest(){
System.out.println("===============>连接数据库成功");
Integer sid=2;
Student student=studentService.getStuById(sid);
System.out.println(student);
}
}
测试结果:
===============>连接数据库成功
Student{sid='2'sname='张三'cid='2'sex='男'birthplace='湖北'}
restful风格GET测试
今天遇到的问题:之前遗留的问题,装过高版本的SDK IDEA项目编译总是提示与版本不符IDEA maven 自动重置java compile 和项目language level的问题
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>8</target>
<source>8</source>
</configuration>
</plugin>
测试数据库连接遇到的问题:
org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer
mybatis-spring的版本过低我用的是1.2.2,改为1.3.2解决问题
现在遗留的问题是在tomcat的中跑项目没用问题,resin 启动报错
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 5 in XML document from class path resource [springmvc-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 240; cvc-elt.1: 找不到元素 'beans' 的声明。
java.lang.UnsupportedClassVersionError: com/jnshu/controller/HelloController has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
今天的收获与总结:经过这2天反复搭建与测试ssm框架整合,了解了ssm框架的使用办法,项目编译使用的jdk版本和各种jar包的依赖关系,3个连接池的连接方法,具体3个连接池的优劣自己还没测试过,都说druid 比较好,暂时就用druid。
明天的目标:
任务2中的
- 16.Post时,对比用Http Body接收参数,用param接收参数的区别,分别描述两种方式的使用场景
- 17.用Spring messageSource 配置错误信息,在接口处做校验,根据错误的类型返回对应的错误信息
评论