zoukankan      html  css  js  c++  java
  • springboot 整合jdbcTemplate

    springboot 整合jdbcTemplate

    〇、搭建springboot环境(包括数据库的依赖)

    一、添加依赖

    如果导入了jpa的依赖,就不用导入jdbctemplete的依赖了jpa的依赖:

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

    如果没有导入jpa的包则要导入jdbcTemplete的包:

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

    二、配置文件

    ########################################################
    ###datasource
    ########################################################
    spring.datasource.url = jdbc:mysql://localhost:3306/mine
    spring.datasource.username = root
    spring.datasource.password = root
    spring.datasource.driverClassName = com.mysql.jdbc.Driver
    spring.datasource.max-active=20
    spring.datasource.max-idle=8
    spring.datasource.min-idle=8
    spring.datasource.initial-size=10
    

    三、持久层

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    import com.xujie.pojo.User;
    
    @Repository
    public class UserDao {
    	
    	@Autowired
    	private JdbcTemplate jdbcTemplate;
    	
    	public void save(User user) {
    		String sql = "insert into user (uname) values ('"+user.getUname()+"')";
    		this.jdbcTemplate.update(sql);
    	}
    }
    

    四、service

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.xujie.dao.UserDao;
    import com.xujie.pojo.User;
    import com.xujie.service.UserService;
    
    @Service
    public class UserServiceImpl implements UserService {
    	
    	@Autowired
    	private UserDao userDao;
    
    	@Override
    	public void save(User user) {
    		this.userDao.save(user);
    	}
    
    }
    

    五、Controller

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.xujie.pojo.User;
    import com.xujie.service.UserService;
    
    @RestController
    public class UserController {
    	
    	@Autowired
    	private UserService userService;
    	
    	@GetMapping("/save")
    	public void save() {
    		User user = new User();
    		user.setUname("yuanxiliu");
    		this.userService.save(user);
    	}
    }
  • 相关阅读:
    android 网络通信 okHttp
    android网络通信(1)
    Java 网络编程
    Android中实现TCP和UDP传输实例
    android保存图片到本地并可以在相册中显示出来
    [android]Android中图形图片及处理相关Api的小总结
    set android app lanuage programatically
    android怎么在不同环境设置下选择不同的resource呢
    MySQL修改root密码的多种方法
    Fragment 与Fragment沟通
  • 原文地址:https://www.cnblogs.com/xujie09/p/8466951.html
Copyright © 2011-2022 走看看