import com.example.demo2.com.example.dao.ShopDao; import com.example.demo2.com.example.entity.Shops; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class ShopController { @Autowired private ShopDao shopdao; /* * 查询 * */ @RequestMapping("sele") @ResponseBody public List test(){ List<Shops> finl = shopdao.finalAll(); for (Shops shop : finl){ System.out.println(shop.getNames()); System.out.println(shop.getPass()); } return finl; } /* * 添加 * */ @RequestMapping("add") @ResponseBody public String test1(String names,String pass,String sex){ Shops shop = new Shops(); shop.setNames(names); shop.setPass(pass); shop.setSex(sex); Integer result = shopdao.add(shop); String st = "添加失败"; if(result>0){ st = "添加成功"; } return st; } /* * 修改 * */ @RequestMapping("upda") @ResponseBody public String test2(Integer id,String names,String pass,String sex){ Shops shop = new Shops(); shop.setId(id); shop.setNames(names); shop.setPass(pass); shop.setSex(sex); Integer result = shopdao.updates(shop); String st = "修改失败"; if(result>0){ st = "修改成功"; } return st; } /* * 删除 * */ @RequestMapping("dele") @ResponseBody public String test3(Integer id){ Integer result = shopdao.deletes(id); String st = "删除失败"; if(result>0){ st = "删除成功"; } return st; } }
dao层
import com.example.demo2.com.example.entity.Shops; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper//声明是一个Mapper,与springbootApplication中的@MapperScan二选一写上即可 public interface ShopDao { //查询 public List<Shops> finalAll(); //添加 public Integer add(Shops shop); //修改 public Integer updates(Shops shop); //删除 public Integer deletes(Integer id); }
mapper
<?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.example.demo2.com.example.dao.ShopDao"> <select id="finalAll" resultType="com.example.demo2.com.example.entity.Shops"> select * from first </select> <insert id="add" parameterType="com.example.demo2.com.example.entity.Shops" useGeneratedKeys="true"> INSERT INTO first(names,pass,sex) values(#{names},#{pass},#{sex}) </insert> <update id="updates" parameterType="com.example.demo2.com.example.entity.Shops"> update first set names =#{names},pass=#{pass},sex=#{sex} where id = #{id} </update> <delete id="deletes" parameterType="com.example.demo2.com.example.entity.Shops"> delete from first where id =#{id} </delete> </mapper>
application.yml
server: port: 8088 context-path: / spring: profiles: active: dev application: name: demo-2 jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 default-property-inclusion: non_null datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8 username: root password: root # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select ‘x‘ testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: typeAliasesPackage: com.example.demo2.com.example.dao mapperLocations: classpath:mapper/*.xml
项目启动文件
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication @EnableTransactionManagement//开启事务管理 @MapperScan("com.example.demo2.com.example.dao")//与dao层的@Mapper二选一写上即可(主要作用是扫包) public class Demo2Application { public static void main(String[] args) { SpringApplication.run(Demo2Application.class, args); } }
pom文件
<?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.example</groupId> <artifactId>demo-2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo-2</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!-- 这个可以使用和spring boot的mybatis两个选一 --> <!--<mybatis.version>3.3.1</mybatis.version>--> <!--<mybatis.spring.version>1.2.4</mybatis.spring.version>--> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--Mybatis 二选一 --> <!--<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency>--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- Mybatis Generator --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
时间: 2024-10-29 06:27:17