SpringBoot版

使用外部Tomcat来启动程序,而不是用默认的Application的main()来启动。 因为,使用Application的main()来启动,修改页面的时候,刷新不会立即有反应。而是要make一下(Ctrl+F5) 而且还要加入,如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

多多少少有些繁琐。 所以,为了保持原来的开发习惯,使用外部Tomcat来启动程序。

引入依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>org.yun</groupId>
    <artifactId>ssm_springboot_redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ssm_springboot_redis</name>
    <description>Spring Boot + MyBatis + Redis(Ajax)</description>
    
    <!--注意:这是war不是jar -->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <!-- 注意:这里不要用SpringBoot提供的版本 -->
            <version>5.1.41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--mybatis-generator-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>

        <!--整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

        <!--整合pagehelper-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.10</version>
        </dependency>


        <!--将jquery以jar包的形式引入-->
        <!--<script src="${ctx}/webjars/jquery/3.3.1-2/jquery.min.js"></script>-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1-2</version>
        </dependency>

        <!--使用外部Tomcat-->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>-->

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

所有配置统一到application.properties文件中。

#mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm_crud
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.schema=classpath:schema.sql
#spring.datasource.data=classpath:data.sql
#spring.datasource.type=com.zaxxer.hikari.HikariDataSource

#mybatis配置
#mybatis.config-locations=classpath:mybatis/mybatis‐config.xml
mybatis.configuration.map-underscore-to-camel-case=true
#mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=org.yun.ssm.model

#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

#日志配置
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.mybatis=debug
logging.level.org.yun.ssm=debug
#logging.file=log/ssm_springboot_redis.log

#spring-mvc配置
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

#缓存配置
#spring.cache.type=REDIS
#spring.cache.cache-names=redisCache

#redis配置
#spring.redis.host=127.0.0.1
#spring.redis.port=6379
#spring.redis.password=
#spring.redis.timeout=1OOO

#spring.redis.jedis.pool.min-idle=5
#spring.redis.jedis.pool.max-active=10
#spring.redis.jedis.pool.max-idle=10
#spring.redis.jedis.pool.max-wait=10


#开启调试模式
#debug=true

启动类

@MapperScan("org.yun.ssm.dao")  //扫描mapper接口所在包
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

其他变动就没有了,至于代码可参看之前的例子。


接下来,配置redis缓存。

加入依赖

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

实现序列化

public class Dept implements Serializable {
...
}

public class Emp implements Serializable {
...
}

要缓存的实体类要实现序列化。

开启缓存功能

@EnableCaching //开启缓存功能
@MapperScan("org.yun.ssm.dao") //扫描mapper接口所在包
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Service 代码变动

DeptService

//从缓存中查询记录
@Cacheable(value = "dept")
public List<Dept> getAll() {
    return deptMapper.selectByExample(null);
}

EmpService

//从缓存中查询记录
@Cacheable(value = "emp")
public List<Emp> getAll() {
	return empExtMapper.selectWithDept();
}

//将新插入的记录加入到缓存中
@CachePut(value = "emp", key = "#result.id")
public Emp save(Emp emp) {
	empMapper.insertSelective(emp);
	return emp;
}

//从缓存中查询记录
@Cacheable(value = "emp", key = "#id")
public Emp getEmp(Long id) {
	return empMapper.selectByPrimaryKey(id);
}

//将更新的记录加入到缓存中
@CachePut(value = "emp", key = "#result.id")
public Emp update(Emp emp) {
	empMapper.updateByPrimaryKeySelective(emp);
	return emp;
}

//删除指定记录的缓存
@CacheEvict(value = "emp", key = "#id")
public void delele(Long id) {
	empMapper.deleteByPrimaryKey(id);
}

//删除指定记录的缓存
@CacheEvict(value = "dept", key = "#ids")
public void batchDel(List<Long> ids) {
	EmpExample example = new EmpExample();
	example.createCriteria().andIdIn(ids);
	empMapper.deleteByExample(example);
}

//从缓存中查询记录
@Cacheable(value = "emp", key = "#name")
public boolean checkName(String name) {
	EmpExample example = new EmpExample();
	example.createCriteria().andNameEqualTo(name);
	int count = empMapper.countByExample(example);
	return count == 0;
}

EmpMapper.xml中的insertSelective()方法要设置 useGeneratedKeys="true" keyProperty="id" keyColumn="id"

在save和update方法的返回值是Emp而不是void。

单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpService empService;

    @Test
    public void test1() {
        System.out.println(deptService.getAll());
        System.out.println(deptService.getAll());
    }

    @Test
    public void getAll() {
        System.out.println(empService.getAll());
        System.out.println(empService.getAll());
    }

    @Test
    public void getEmp() {
        System.out.println(empService.getEmp(1L));
        System.out.println(empService.getEmp(1L));
    }

    @Test
    public void checkName() {
        System.out.println(empService.checkName("tom"));
        System.out.println(empService.checkName("tom"));
    }

    @Test
    public void save() {
        Emp emp = new Emp();
        emp.setName(UUID.randomUUID().toString());
        emp.setGender("0");
        emp.setEmail(emp.getName() + "@qq.com");
        emp.setDeptId(2L);
        empService.save(emp);
        Long id = emp.getId();
        System.out.println(empService.getEmp(id));
    }

    @Test
    public void update() {
        Emp emp = new Emp();
        emp.setId(302L);
        emp.setName(UUID.randomUUID().toString());
        emp.setGender("1");
        emp.setEmail(emp.getName() + "@qq.com");
        emp.setDeptId(3L);
        empService.update(emp);
        Long id = emp.getId();
        System.out.println(empService.getEmp(id));
    }

    @Test
    public void delele() {
        empService.delele(302L);
        System.out.println(empService.getEmp(302L));
    }

    @Test
    public void batchDel() {
        empService.batchDel(Arrays.asList(73L, 72L));
        System.out.println(empService.getEmp(73L));
        System.out.println(empService.getEmp(72L));
    }
}

转载于:https://my.oschina.net/mondayer/blog/3041485

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值