使⽤spring整合mybatis连接mysql数据库的⼀个完整实例项⽬完整结构:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jnshu</groupId>
<artifactId>wq_task1_spring-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--//定义spring包统⼀版本号-->
<spring.version>4.3.14.RELEASE</spring.version>
<mavenpiler.source>1.8</mavenpiler.source>
<mavenpiler.target>1.8</mavenpiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--Spring.jar start-->
<!--包含Spring框架基本的核⼼⼯具类,Spring其它组件要都要使⽤到这个包⾥的类,是其它组件的基本核⼼-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependency>
<!--jar⽂件是所有应⽤都要⽤到的,它包含访问配置⽂件、创建和管理bean
以及进⾏Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!--为Spring核⼼提供了⼤量扩展。可以到使⽤Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类,
UI⽅⾯的⽤来与模板(Templating)引擎如Velocity、FreeMarker、JasperReports集成的类,以及校验Validation⽅⾯的相关类-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--这个jar⽂件包含对Spring对JDBC数据访问进⾏封装的所有类。-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--Spring表达式语⾔-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!--是spring-context.RELEASE.jar依赖包.是spring事务处理的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!--commons-logging.jar包是使⽤spring的必备包,⽤来记录程序运⾏时的活动的⽇志记录.如果缺少就会报错-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!--这个jar⽂件包含Web应⽤开发时,⽤到Spring框架时所需的核⼼类,包括⾃动载⼊WebApplicationContext特性的类、Struts与JSF集成类、⽂件上传的⽀持类、Filter类和⼤量⼯具辅助类。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!-- 这个jar⽂件包含Spring MVC框架相关的所有类。包含国际化、标签、Theme、
视图展现的FreeMarker、JasperReports、Tiles、Velocity、XSLT相关类。-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<!--spring End-->
<!--⽇志包 Start-->
<!--log4j2依赖包-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<!--slf4j依赖-->
<!--slf4j-api作为⽇志接⼊的接⼝,不负责具体的⽇志实现,只在编译时负责寻合适的⽇志系统进⾏绑定--> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<!--log4j2和slf4j桥接依赖,桥接作⽤-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!--⽇志包 End-->
<!--Mybatis Start-->
<!--Mybatis的核⼼包-->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!--集合spring与mybatis框架的⼯具包-->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!--Mybatis END-->
<!--Mysql数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!--dbcp数据库连接池的jar包,⽤来在l中配置数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
Student实体类:
package com.jnshu.pojo;
public class Student {
//实体类.属性与数据表⼀⼀对应.然后添加set/get⽅法,toString⽅法
private int id;
private String name;
private String major;
private long admission_time;
private String graduate_school;
private long online_id;
private String diary_link;
private String wish;
private String guide;
private long create_time;
private long update_time;
public Student() {
}
public Student(String name, String major, long admission_time, String graduate_school, long online_id, String diary_link, String wish, String guide,long cr eate_time,long update_time) {
this.name = name;
this.major = major;
this.admission_time = admission_time;
this.diary_link = diary_link;
this.wish = wish;
this.guide = guide;
this.update_time=update_time;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public long getAdmission_time() {
return admission_time;
}
public void setAdmission_time(long admission_time) {
this.admission_time = admission_time;
}
public String getGraduate_school() {
return graduate_school;
}
public void setGraduate_school(String graduate_school) {
}
public long getOnline_id() {
return online_id;
}
public void setOnline_id(long online_id) {
}
public String getDiary_link() {
return diary_link;
}
public void setDiary_link(String diary_link) {
this.diary_link = diary_link;
}
public String getWish() {
return wish;
}
public void setWish(String wish) {
this.wish = wish;
}
public String getGuide() {
return guide;
}
public void setGuide(String guide) {
this.guide = guide;
}
public long getCreate_time() {
return create_time;
}
public void setCreate_time(long create_time) {
}
public long getUpdate_time() {
return update_time;
}
public void setUpdate_time(long update_time) {
this.update_time = update_time;
}
mysql下载jar包@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", major='" + major + '\'' +
", admission_time='" + admission_time + '\'' + ", graduate_school='" + graduate_school + '\'' + ", online_id=" + online_id +
", diary_link='" + diary_link + '\'' +
", wish='" + wish + '\'' +
", guide='" + guide + '\'' +
", create_time='" + create_time + '\'' +
", update_time='" + update_time + '\'' +
'}';
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论