前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot集成mybatis+jsp项目结构: pom配置文件如果想将mapper文件和dao层放在一起如下图:将配置文件修改为

springboot集成mybatis+jsp项目结构: pom配置文件如果想将mapper文件和dao层放在一起如下图:将配置文件修改为

作者头像
用户5899361
发布2020-12-07 14:16:04
6020
发布2020-12-07 14:16:04
举报
文章被收录于专栏:学习java的小白

项目结构:

pom配置文件

代码语言:javascript
复制
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
  </parent>
  <dependencies>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--jsp页面使用jstl标签-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
    </dependency>

    <!--用于编译jsp-->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <scope>provided</scope>
    </dependency>
    <!--引入mysql依赖-->
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
    <!-- 去除模板引擎  引用jsp-->
    <!--<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>1.4.0.RELEASE</version>
    </dependency>-->
      <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.5.1.RELEASE</version>
      </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <!--<scope>provided</scope>-->
    </dependency>

    <dependency>
      <groupId>net.sourceforge.nekohtml</groupId>
      <artifactId>nekohtml</artifactId>
      <version>1.9.22</version>
    </dependency>

    <!--引入mybatis依赖-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.1</version>
    </dependency>

  </dependencies>

2.dao层

代码语言:javascript
复制
package com.dao;

import com.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 因为springboot 集成jpa写sql需要继承extends JpaRepository<User,Long>
 *     mybatis不需要  所以注释掉
 */
@Repository
public interface UserDao /*extends JpaRepository<User,Long> */{


    @Select(" select * from User  ")
   // @Query("from user")   jpa语句
    List<User> getAllUser();

    @Insert("insert into user(name,password) VALUES(#{name},#{password})")
    int addUser(User user);
}

3.service

代码语言:javascript
复制
package com.userService;

import com.dao.UserDao;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@org.springframework.stereotype.Service
public class UserServiceImpl implements UserService {

    @Autowired
   private UserDao userdao;

    @Override
    public List<User> getUserList() {
        List<User> list=userdao.getAllUser();
        return list;
    }

    @Override
    public int addUser(User user) {
        int result=userdao.addUser(user);
        return result;
    }
}

4.Controller

代码语言:javascript
复制
package com.coller;

import com.pojo.User;
import com.userService.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/")
    public String index(Model model){

        List<User> list=userService.getUserList();
        model.addAttribute("user",list);
        return "/test";
    }


    @ResponseBody
    @RequestMapping("add")
    public String add(User user){
       int i= userService.addUser(user);
       System.out.print(i);
        return "1" ;
    }
}

5.配置文件为application.properties类型的如下

代码语言:javascript
复制
server.port=8888
server.tomcat.uri-encoding=utf-8

#MySQL
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#Spring Data JPA
spring.jpa.database=MYSQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

#springmvc
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

application.yml的配置文件如下

代码语言:javascript
复制
server:
  #端口号
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp


mybatis:
 # mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.bdqn.pojo

6.启动类

代码语言:javascript
复制
package com.Test;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan(basePackages = "com")
@MapperScan("com.dao")
public class hehe  extends SpringBootServletInitializer {



    public static void main(String[] args) throws Exception {
        SpringApplication.run(hehe.class, args);
    }
}

7.jpa注解扫描类(如果用mybatis可以不写这个类)

代码语言:javascript
复制
package com;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories(basePackages = "com.dao")
@EntityScan(basePackages = "com.pojo")
public class JpaConfiguration {
    @Bean
    PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

如果想将mapper文件和dao层放在一起如下图:

将配置文件修改为

代码语言:javascript
复制
server:
  #端口号
  port: 8080

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: XXX
    password: XXX
    url: jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp


mybatis:
 #指定实体类位置
  type-aliases-package: com.bdqn.pojo

pom文件中<build>标签中添加如下配置

代码语言:javascript
复制
<!--指定mapper存放路径-->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
      </resource>
      <!--指定配置文件存放路径-->
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
            <include>**/*.yml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目结构:
  • pom配置文件
  • 如果想将mapper文件和dao层放在一起如下图:
  • 将配置文件修改为
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档