文章目录
建表
新建项目,引入jar包
src.main.java
entity 实体类
dao 接口
service
controller
resources
spring.xml
web
web.xml
index.jsp
save.jsp
user.jsp
部署Tomcat
建表
快速建一个userinfos表,并插入两条数据
show databases; drop database if exists mydemo; create database mydemo; use mydemo; create table userinfos( userid int primary key not null auto_increment, username varchar(20) not null, birthday date not null); show tables; insert into userinfos(username,birthday) values ('aaa','1996-9-9'); insert into userinfos(username,birthday) values('bbb','1996-9-9'); select * from userinfos;
新建项目,引入jar包
项目结构预览
由于40分钟没能搓完,这会儿已经ssmdemo6了!,想要框架玩的6,基础功必须扎实!!
pom.xml
<?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>com.kgc</groupId> <artifactId>ssmdemo2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>ssmdemo2 Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <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> <spring-version>4.3.7.RELEASE</spring-version> <json-version>2.9.8</json-version> </properties> <!-- 单元测试 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 2.数据库 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- DAO: 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>1.3.2</version> </dependency> <!-- 3.Servlet web --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <!--provided表明该包只在编译和测试的时候用,因为tomcat也有servlet-api包, 此处为了防止冲突--> <scope>provided</scope> </dependency> <!--jsp标签库--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Json--> <!--序列化,将对象转换成json--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${json-version}</version> </dependency> <!--反序列化--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${json-version}</version> </dependency> <!--注释语法 使json按按要求返回信息--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${json-version}</version> </dependency> <!-- 4.Spring --> <!-- 1)Spring核心 --> <!--核心容器,提供基本框架--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <!--负责实现Spring框架的IOC模块--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <!--spring应用上下文--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <!-- 2)Spring DAO层 --> <!--分离业务逻辑和数据库交互代码--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <!--实现事务管理--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> <!-- 3)Spring web --> <!--分离事物逻辑视图控制器--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> </dependencies> <build> <finalName>ssmdemo2</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> <!--解决maven国内慢--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </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> </plugins> </pluginManagement> <!--mapper产生对应的xml文件--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
src.main.java
entity 实体类
package com.kgc.ssmdemo2.entity; import java.io.Serializable; import java.util.Date; /** * @author:J * @date:2019/11/13 * @aim: */ public class Userinfos implements Serializable { private int userid; private String username; private Date birthday; public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
dao 接口
package com.kgc.ssmdemo5.dao; import com.kgc.ssmdemo5.entity.User; import org.apache.ibatis.annotations.*; import java.util.List; public interface UserDAO { @Insert("insert into userinfos(username,birthday) values (#{username},#{birthday}) ") public void insertUser(User user); @Delete("delete from userinfos where userid=#{userid}") public void delUser(@Param("userid") int userid); @Update("update userinfos set username=#{username},birthday=#{birthday} where userid=#{userid}") public void modUser(User user); @Select("select * from userinfos") public List<User> findAll(); @Select("select * from userinfos where userid=#{userid}") public User findById(@Param("userid") int userid); }
这里用了几个注解的小操作,省去了后面写usermapper.xml
和mybatis-config.xml
,距离40分钟搓完的小目标又近了一步,哈哈!
service
package com.kgc.ssmdemo2.service; import com.kgc.ssmdemo2.dao.UserDAO; import com.kgc.ssmdemo2.entity.Userinfos; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * @author:J * @date:2019/11/13 * @aim: */ @Service @Transactional public class UserService { @Autowired private UserDAO userDAO; public void saveUser(Userinfos user){ userDAO.insertUser(user); } public void deUser(int userid){ userDAO.delUser(userid); } public void modUser(Userinfos user){ userDAO.modUser(user); } public List<Userinfos> findAll(){ return userDAO.findAll(); } public Userinfos finByid(int userid){ return userDAO.finByid(userid); } }
controller
package com.kgc.ssmdemo2.controller; import com.kgc.ssmdemo2.entity.Userinfos; import com.kgc.ssmdemo2.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.Date; /** * @author:J * @date:2019/11/13 * @aim: */ @Controller public class InitCtrlk { @Autowired private UserService userService; /** *接受前端页面传送出来的日期类型 并进行转换 */ @InitBinder public void dateHanlder(WebDataBinder binder){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); sdf.setLenient(true); binder.registerCustomEditor(Date.class,new CustomDateEditor(sdf,true)); } @RequestMapping("/init.do") public String init(HttpServletRequest request){ request.setAttribute("infos",userService.findAll()); return "index.jsp"; } @RequestMapping("/save.do") public String save(Userinfos user){ userService.saveUser(user); return "redirect:/init.do"; } @RequestMapping("/del.do") public String del(int userid){ userService.deUser(userid); return "redirect:/init.do"; } @RequestMapping("/mod.do") public String mod(Userinfos user){ userService.modUser(user); return "redirect:/init.do"; } @RequestMapping("/single.do") public String single(int userid,HttpServletRequest request){ request.setAttribute("user",userService.finByid(userid)); return "user.jsp"; } }
resources
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 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/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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--开启自动配置--> <context:annotation-config></context:annotation-config> <!-- 扫描项目下所有使用注解的类型 --> <context:component-scan base-package="com.kgc.ssmdemo2"></context:component-scan> <!-- 2.数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 配置连接池属性 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost/mydemo"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="initialSize" value="3"></property> <property name="maxActive" value="30"></property> <property name="maxWait" value="30000"></property> <property name="testOnBorrow" value="true"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> <!--整合spring和mybatis--> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置mybatis全局配置文件:mybatis.cfg.xml --> <!--<property name="configLocation" value="classpath:mybatis.cfg.xml"></property>--> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <!--<property name="mapperLocations" value="classpath*:mapper/*.xml"></property>--> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 --> <bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.kgc.ssmdemo2.dao"></property> </bean> </beans>