Java|java教程
boot+jdbctemplate+sql,spring,server
Java-java教程
手机竞拍APP源码,ubuntu怎么回桌面,tomcat是一种容器,java正则 爬虫,php生成超链接代码,xh的seolzw
前言
小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致。 在spring boot中使用jdbc 连接sql server数据只需要引入两个jar:spring-boot-starter-jdbc、spring-boot-starter-data-jpa
护卫神源码,调整ubuntu系统时间,python爬虫代码排行,php 空格屏蔽,seo15.lzw
项目结构
和上篇spring boot入门保持相同
安卓6.0远控源码,ubuntu系统连接热点,tomcat怎么看线程,爬虫常识大全,微社区 php源码,免费seo学习课程seo博客lzw
pom.xml
org.springframework.boot spring-boot-starter-web <!----> <!----><!--org.springframework.boot--><!--spring-boot-starter-tomcat--> <!----> <!---->org.springframework.boot spring-boot-starter-cacheorg.springframework.boot spring-boot-starter-jdbcorg.springframework.boot spring-boot-starter-data-jpa 1.5.4.RELEASE
Application.java、Dao、Service、model
1、Application.java
@SpringBootApplication(scanBasePackages = "com.autohome")public class Application extends SpringBootServletInitializer{public static void main(String[] args){ System.out.println("server is running at 8080...."); SpringApplication.run(Application.class,args); } @Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(Application.class); }}
2、Dao
package com.autohome.dao;import com.autohome.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.PreparedStatementSetter;import org.springframework.stereotype.Repository;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Types;import java.util.List;@Repositorypublic class UserDao { @Autowired JdbcTemplate jdbcTemplate; public List listAllUser() { List list = jdbcTemplate.query("select * from t_userinfo",new User()); return list; } public int insertUser(final User user) { int result = jdbcTemplate.update("insert into t_userinfo (name,address) VALUES (?,?)", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException {ps.setString(1,user.getName());ps.setString(2,user.getAddress()); } }); return result; } public int updateUser(final User user) { int result = jdbcTemplate.update("UPDATE t_userinfo set name=?,address=? where id=?", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException {ps.setString(1,user.getName());ps.setString(2,user.getAddress());ps.setInt(3,user.getId()); } }); return result; } public int deleteUser(int id) { int result = jdbcTemplate.update("delete from t_userinfo where id=?",new Object[]{id},new int[]{Types.INTEGER}); return result; }}
3、Service
package com.autohome.service;import com.autohome.dao.UserDao;import com.autohome.model.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserService { @Autowired UserDao userDao; public List listAllUser(){ return userDao.listAllUser(); } public int insertUser(User user){ return userDao.insertUser(user); } public int updateUser(User user){ return userDao.updateUser(user); } public int deleteUser(int id){ return userDao.deleteUser(id); }}
4、Controller
package com.autohome.controller;import com.autohome.model.User;import com.autohome.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;/** * Created by zhangfei on /6/22. */@Controller@RequestMapping("/user")public class UserController { @ResponseBody @RequestMapping("/detail") public User detail(Integer id){ User user=new User(); user.setId(id); user.setName("zhangsan"); user.setAddress("china"); return user; } @Autowired UserService userService; @ResponseBody @RequestMapping("/list") public List list(){ List list = userService.listAllUser(); System.out.println("size:"+list.size()); return list; } @RequestMapping(value="/insert",method = RequestMethod.POST) public String insertUser(String name,String address){ User user =new User(); user.setName(name); user.setAddress(address); int result = userService.insertUser(user); if(result>0){ return "{\"returncode\":0,\"message\":\"success\"}"; }else{ return "{\"returncode\":0,\"message\":\"error\"}"; } }}