赞
踩
这里使用Spring Boot实现了一个简单的项目,能够实现简单的数据库操作,工具使用的是IntelliJ IDEA2017.3,数据库工具使用的是navicat for Sql Server,语言使用的Java。
1.新建一个空的Maven项目
2.导入需要的jar包
pom.xml:
<?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.test</groupId> <artifactId>MavenWebProject02</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- tomcat的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <!-- 引入jdbc的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- 引入SQL Server的jar包 --> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> </dependencies> <!-- 以下为阿里云的仓库链接 --> <repositories> <repository> <id>aliyun-repos</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
注:此处遇到了Failuer to find com.microsoft.sqlserver:sqljdbc4:jar:4.0的问题,maven项目引入sqljdbc4 找不到包,添加了【以下为阿里云的仓库链接】下面的配置后就不报错了,此处原因不明。
3.新建数据库并配置数据库信息
我们使用navicat for sql server新建一个测试库test01,然后新建一个表t_test,此处需要注意,标识这里需要勾选,navicat for sql server在只能在新建表的时候设置自动递增,就是勾选此处的标识:
我们的数据相关配置信息application.properties:数据库名为test01,用户名为sa,密码为sa123456:
server.port=8081
#数据库用户名密码设置
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc\:sqlserver\://127.0.0.1\:1433;databaseName\= test01
spring.datasource.username=sa
spring.datasource.password=sa123456
#数据库连接池配置
spring.datasource.tomcat.max-active=20
spring.datasource.tomcat.max-idle=8
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.initial-size=10
4.相关代码
User.java:
public class User { private Long id; private String name; public User(Long id, String name) { this.id = id; this.name = name; } public User(String name) { this.name = name; } public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
UserDao.java:
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(User user){
jdbcTemplate.update("insert into t_user(name) values(?)",user.getName());
}
public User get(Long id){
RowMapper<User> rowMapper = new BeanPropertyRowMapper <User>(User.class);
User user = jdbcTemplate.queryForObject("select * from t_user where id = ?",rowMapper,id);
return user;
}
}
UserController.java:
@RestController public class UserController { @Autowired private IUserService userService; @RequestMapping("/user/hello") public String hello(){ return "hello"; } @RequestMapping("/user/save") public String save(String name){ User user = new User(name); userService.save(user); return "saved successfully"; } @RequestMapping("/user/get") public User get(Long id){ User user = userService.get(id); return user; } }
IUserService接口:
public interface IUserService {
void save(User user);
User get(Long id);
}
UserServiceImpl.java:
@Service
public class UserServiceImpl implements IUserService{
@Autowired
private UserDao dao;
@Override
public void save(User user) {
dao.save(user);
}
@Override
public User get(Long id) {
return dao.get(id);
}
}
主方法App.java:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
工程结构如下:
5.程序执行效果展示
App.java方法启动成功后,我们在浏览器中输入http://localhost:8081/user/save?name=mytest,可以看到如下执行结果:
我们查看数据库(测试sql语句的时候插入了两条数据,所以此处id为3):
我们在浏览器中输入http://localhost:8081/user/get?id=3,可以看到如下结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。