Spring+SpringMVC+ Mybatis(Spring Tools Suites 3) 简单项目
作者:互联网
STS 3
tomcat 9.0,
JDK 8
一、项目结构
二、项目依赖
三、创建项目前生成web.xml文件
项目右键--》Java EE Tools-->Generate Deployment Descriptor Stub-->生成web.xml文件
四、创建项目代码
1.创建实体类
例如:Dept.java
package com.dxj.entity; import java.sql.Timestamp; import org.apache.ibatis.type.Alias; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * 部门 Dept * @author Administrator * */ @Alias("Dept") @Data @AllArgsConstructor @NoArgsConstructor public class Dept { private Integer id; private String name; private String loc; private Timestamp date; private String person; }
2.创建数据持久层Mapper
例如:DeptMapper.java
package com.dxj.mapper import java.util.List; import org.apache.ibatis.annotations.Param; import com.dxj.entity.Dept; /** * 部门 测试方法 * @author Administrator * */ public interface DeptMapper { void save(Dept dept); List<Dept> findAll(); void update(@Param("upd")Dept dept); void del(Integer id); }
3.创建服务层
例如:IDeptService.java
package com.dxj.service; import java.util.List; import org.apache.ibatis.annotations.Param; import com.dxj.entity.Dept; /** * 部门 服务接口 * @author Administrator * */ public interface IDeptService { void save(Dept dept); List<Dept> findAll(); void update(@Param("upd")Dept dept); void del(Integer id); }
IDeptServiceImpl.java
package com.dxj.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.dxj.entity.Dept; import com.dxj.mapper.DeptMapper; import com.dxj.service.IDeptService; /** * 部门 服务实现 * @author Administrator * */ @Service("ids") @Transactional(propagation = Propagation.SUPPORTS) public class DeptServiceImpl implements IDeptService { @Autowired private DeptMapper iDeptMapper; @Override public List<Dept> findAll() { return iDeptMapper.findAll(); } @Override public void save(Dept dept) { iDeptMapper.save(dept); } @Override public void update(Dept dept) { iDeptMapper.update(dept); } @Override public void del(Integer id) { iDeptMapper.del(id); } }
4.创建前后端交互层Controller
例如:DeptController.java
package com.dxj.controllers; import java.sql.Timestamp; import java.util.Date; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.dxj.entity.Dept; import com.dxj.service.IDeptService; /** * 部门 * @author Administrator * */ @Controller public class DeptController { @Autowired private IDeptService ids; @RequestMapping("/findAll.action") @ResponseBody public List<Dept> findAll(){ return ids.findAll(); } @RequestMapping("/save.action") @ResponseBody public String save(Dept dept) { ids.save(new Dept(null, "dxj", "308", new Timestamp(new Date().getTime()), "summer")); return "添加成功"; } @RequestMapping("/update.action") @ResponseBody public String update(Dept dept) { ids.update(new Dept(2, "hf", null, null, null)); return "修改成功"; } @RequestMapping("/del.action") @ResponseBody public String del(Integer id) { ids.del(4); return "删除成功"; } }
五、配置文件
1.springmvc-servlet.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <context:component-scan base-package="com.dxj.controllers" /> <mvc:annotation-driven /> <mvc:resources location="/static/" mapping="/static/**"/> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10240000" /> <property name="maxUploadSizePerFile" value="1024000" /> </bean> </beans>
2.mysql.properties
druid.url=jdbc:mysql://localhost:3306/hospital?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true druid.username=root druid.password=1234 druid.driverClassName=com.mysql.jdbc.Driver
3.xx.mapper.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="com.dxj.mapper.DeptMapper"> <resultMap id="DeptMap" type="Dept"> <id column="de_id" jdbcType="INTEGER" property="id" /> <result column="de_name" jdbcType="VARCHAR" property="name" /> <result column="de_loc" jdbcType="VARCHAR" property="loc" /> <result column="de_time" property="date" /> <result column="de_by" jdbcType="VARCHAR" property="person" /> </resultMap> <!-- 增加 --> <insert id="save"> INSERT INTO dept_tab(de_name,de_loc,de_time,de_by) VALUES(#{name},#{loc},#{date},#{person}) </insert> <!-- 全查询 --> <select id="findAll" resultMap="DeptMap"> SELECT * FROM dept_tab </select> <!-- 修改--> <update id="update"> UPDATE dept_tab <set> <if test="upd.name!=null"> de_name=#{upd.name} </if> <if test="upd.loc!=null"> de_loc=#{upd.loc} </if> <if test="upd.date!=null"> de_time=#{upd.date} </if> <if test="upd.person!=null"> de_by=#{upd.person} </if> </set> WHERE de_id=#{upd.id} </update> <!-- 删除--> <delete id="del"> DELETE FROM dept_tab WHERE de_id=#{id} </delete> </mapper>
4.configuration.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="jdbcTypeForNull" value="NULL" /> </settings> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!-- 使用下面的方式配置参数,后面会有所有的参数介绍 --> <property name="helperDialect" value="mysql"/> <property name="reasonable" value="true"/> </plugin> </plugins> </configuration>
5.spring-mybatis.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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <context:component-scan base-package="com.dxj"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 读取mysql的配置文件 --> <context:property-placeholder location="classpath:mybatis/config/mysql.properties"/> <!-- 配置数据源 Druid--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="url" value="${druid.url}"/> <property name="username" value="${druid.username}"/> <property name="password" value="${druid.password}"/> <property name="driverClassName" value="${druid.driverClassName}"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- configuration.xml的位置 --> <property name="configLocation" value="classpath:mybatis/configuration.xml"/> <!-- 引用数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 扫描mapper.xml文件 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.mapper.xml"/> <property name="typeAliasesPackage" value="com.dxj.entity"/> </bean> <!-- mybatis的辅助配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mybatis操作接口的位置 --> <property name="basePackage" value="com.dxj.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 事务管理 --> <bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:annotation-driven transaction-manager="txManger"/> </beans>
6.log4j.properties
log4j.rootLogger=debug,stdout,logfile #console output log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.stdout.PatternLayout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}<-->%F<-->%p<-->%m%n #file output log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=F:/logs/mybatis/log.log log4j.appender.logfile.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile.PatternLayout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}<-->%F<-->%p<-->%m%n #mybatis log log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
标签:Suites,java,SpringMVC,Spring,Dept,org,import,com,log4j 来源: https://www.cnblogs.com/dxjx/p/12461692.html