使用mave项目,实现简单地从页面到数据库的增删改查
1,整体目录
2,需要的依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.9.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.11</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency>
3,编写数据库
4,先编写实体类,pojo中的Monkey.java,与数据库中的字段相应
package pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Monkey { private Integer id; private String name; private String address; private String email; }
5,从底层向上原则,先编写dao层,接口和实现接口的xml文件最好在一个目录下
MonkeyI接口
package dao; import pojo.Monkey; import java.util.List; public interface MonkeyI { //添加一只猴子 public int insert(Monkey monkey); //根据id删除一只猴子 public int delete(int id); //更新 public int update(Monkey monkey); //获取所有猴子 public List<Monkey> select(); }
实现类MonkeyImpl.xml文件,
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="dao.MonkeyI"> <insert id="insert" parameterType="Monkey"> insert into monkey values (#{id},#{name},#{address},#{email}) </insert> <delete id="delete" parameterType="int"> delete from monkey where id = #{id} </delete> <update id="update" parameterType="Monkey"> update monkey set name = #{name},address = #{address},email=#{email} where id =#{id} </update> <select id="select" resultType="Monkey"> select * from monkey </select> </mapper>
6,配置dao层的mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <typeAliases> <typeAlias type="pojo.Monkey" alias="Monkey"></typeAlias> </typeAliases> <mappers> <mapper resource="dao/MonkeyI.xml"/> </mappers> </configuration>
7,配置数据库连接文件db.properties
jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/ssm?useSSL=true&serverTimezone=GMT%2B8 jdbc.user = root jdbc.password = zhs03171812
8,将mybatis-config.xml文件交于spring管理,创建spring-dao.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" 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"> <!--扫描包--> <context:component-scan base-package="dao"></context:component-scan> <!--导入资源文件--> <context:property-placeholder location="classpath:qwq.properties"></context:property-placeholder> <!--配置c3p0连接池--> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置SqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <!--获取连接池--> <property name="dataSource" ref="dataSource"></property> <!--获取XML文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--配置dao要扫描的包,将dao接口动态的注入到spring中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer"> <!--获取SqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> <!--注入要扫描的包--> <property name="basePackage" value="dao"></property> </bean> </beans>
9,配置services层,在该包下创建业务类以及接口
MonkeyServicesI接口
package services; import pojo.Monkey; import java.util.List; public interface MonkeyServicesI { //添加一只猴子 public int insert(Monkey monkey); //根据id删除一只猴子 public int delete(int id); //更新 public int update(Monkey monkey); //获取所有猴子 public List<Monkey> select(); }
MonkeyServicesImpl实现类
package services; import dao.MonkeyI; import pojo.Monkey; import java.util.List; public class MonkeyServicesImpl implements MonkeyServicesI { private MonkeyI monkeyI; public MonkeyI getMonkeyI() { return monkeyI; } public void setMonkeyI(MonkeyI monkeyI) { this.monkeyI = monkeyI; } @Override public int insert(Monkey monkey) { return monkeyI.insert(monkey); } @Override public int delete(int id) { return monkeyI.delete(id); } @Override public int update(Monkey monkey) { return monkeyI.update(monkey); } @Override public List<Monkey> select() { return monkeyI.select(); } }
10,将services交给spring管理,编写spring-services.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" 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"> <!--扫描的包--> <context:component-scan base-package="services"></context:component-scan> <!--由于存在ref对象--> <bean id="monkeyServicesImpl" class="services.MonkeyServicesImpl"> <property name="monkeyI" ref="monkeyI"></property> </bean> <!--声明事务配置--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
11,配置Controller层,即编写spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" 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"> <!--获取处理器的适配器和解析器--> <mvc:annotation-driven></mvc:annotation-driven> <!--过滤非文本文件--> <mvc:default-servlet-handler></mvc:default-servlet-handler> <!--获取视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver"> <property name="prefix" value="/WEB-INf/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
12,在applicationContext.xml中导入spring-mvc.xml,spring-services.xml,spring-dao.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="classpath:spring-dao.xml"></import> <import resource="classpath:spring-mvc.xml"></import> <import resource="classpath:spring-services.xml"></import> </beans>
13,在web-info下增加一个jsp文件夹,用于编写jsp界面
14,测试业务层以及dao层是否成功可以访问数据库
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pojo.Monkey; import services.MonkeyServicesI; public class Test { @org.junit.Test public void test(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); MonkeyServicesI monkeyServicesImpl = app.getBean("monkeyServicesImpl", MonkeyServicesI.class); int i = monkeyServicesImpl.insert(new Monkey(1,"齐天大圣","花果山","123@qq.com")); System.out.println(i); } }
运行结果;
连接成功!并且往数据库中增加了一只猴子的信息
15,测试controller,在controller中新建MonkeyCon类,查询全部的猴子信息,并且展示在页面上
package controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import pojo.Monkey; import services.MonkeyServicesImpl; import java.util.List; @Controller //浏览器访问的映射路径:(/t1/c1) @RequestMapping("/t1") public class MonkeyCon { @Autowired @Qualifier("monkeyServicesImpl")//表明注入的是具体的哪一个实现类的bean private MonkeyServicesImpl monkeyServices; @RequestMapping("/c1") public String get(Model model){ //用于拼接前面的前缀后缀 List<Monkey> list = monkeyServices.select(); model.addAttribute("list",list); return "test"; } }
16,配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <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" metadata-complete="true"> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>Filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
17,新建test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <div align="center"> #{list} </div> </body> </html>
18编辑tomcat
19,运行tomcat
最终运行成功!
总结:要注意路径的问题,尤其是资源的的导入路径,以及引用路径!