eclipse整合springboot+mybatis

网上看了很多资料,于是我还是选择离线下载一个sts插件,因为我在eclipse上面完全没弄起,下载好了sts插件(我的是sts-3.9.3.RELEASE)进去里面点击这个程序
直接点击,然后会提示你打开eclipse,然后跟着提示走就行了。

新建 项目 new--》spring starter Project

jdk要1.8的,点击next



楼主目前用的mysql数据库,所以选的mysql。mybatis

然后选择web【其他根据自己情况勾选】, 然后一直到下一步,直到finish。


接来下,我们要将application.properties 改成application.yml


【这里啰嗦一句,最开始我自己建项目的时候遇到一个神坑,建好项目就是红色感叹号?结果是网不好,jar没下载完,然后自己把相应的jar删掉,重新下载就好了。并且保证自己的jdk查询的javac-version 和java -version 版本号 保持一致,我用的springboot 2.0的。所以jdk还要1.8 的】

项目结构

../src/main/java/
com.example.demo.controller
com.example.demo.dao
com.example.demo.domain
com.example.demo.services

../src/main/resources/
mapper/HappinessMapper.xml


配置application.yml


代码为:
#配置数据源
spring:
  datasource:
     url: jdbc:mysql://127.0.0.1:3306/dianping?useUnicode=true&characterEncoding=utf8
     username: root
     password: 123456
     driver-class-name: com.mysql.jdbc.Driver
     
#使用 yml的方式
mybatis:
  type-aliases-package: com.example.demo.domain
  mapper-locations: ['mapper/*.xml']



测试内容写入:

controller:

@RestController
@RequestMapping("/demo")
public class HappinessController {
    @Autowired
       privateHappinessService happinessService;

      @RequestMapping("/query")
       publicHappiness testQuery(){
          return happinessService.selectService("北京");
       }

      @RequestMapping("/insert")
       publicHappiness testInsert(){
          happinessService.insertService();
          return happinessService.selectService("西安");
       }
}

Dao:

@Mapper
public interface HappinessDao {
    HappinessfindHappinessByCity(String city);
    intinsertHappiness(Map map);
}

domain实体类:

public class Happiness {
    private Long id;
       privateString city;
       privateInteger num;
       public LonggetId() {
          returnid;
       }
       public voidsetId(Long id) {
          this.id =id;
       }
       publicString getCity() {
          returncity;
       }
       public voidsetCity(String city) {
          this.city =city;
       }
       publicInteger getNum() {
          returnnum;
       }
       public voidsetNum(Integer num) {
          this.num =num;
       }
      
}

service:

@Service
public class HappinessService {
 
   @Autowired
       privateHappinessDao happinessDao;

       publicHappiness selectService(String city){
          return happinessDao.findHappinessByCity(city);
       }

      @Transactional
       public voidinsertService(){
          Map map=newHashMap<>();
         map.put("city", "西安");
         map.put("num", 9421);
          happinessDao.insertHappiness(map);
         // int a = 1 / 0; //模拟故障
          map.clear();
          map.put("city", "北京");
         map.put("num", 9421);
          happinessDao.insertHappiness(map);
       }
}

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.example.demo.dao.HappinessDao">
    <select id="findHappinessByCity" parameterType="String" resultType="com.example.demo.domain.Happiness">
        SELECT * FROM happiness WHERE city = #{city}
    </select>

    <insert id="insertHappiness" parameterType="HashMap" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO happiness(city, num) VALUES(#{city}, #{num})
    </insert>
</mapper>
运行项目: 点击项目右键Run --->run Configurations---> Goals 填入spring-boot:run

访问项目的路径为:http://127.0.0.1:8080/demo/insert  根据controller路径跳转查看
       
阅读更多
上一篇将Tomcat注册为电脑系统服务
下一篇maven 配置generatorConfig.xml 自动生成代码文件
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭