1.使用jdk8 ,tomcat8
2.在eclipse中创建一个maven项目,手动配置springMvc.xml以及web.xml,项目结构如下:
3.pom文件引入spring的web包和mysql包
pom.xml文件内容:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
4.手动配置jdbc.properties文件
jdbc.properties文件内容:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/pi_develop?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
最新的spring需要在jdbcUrl后面加入
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
5.配置springMvc.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd ">
<!-- 组件扫描,扫描含有注解的类 -->
<context:component-scan base-package="io.app"></context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 指定连接数据库的驱动-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<!-- 指定连接数据库的URL-->
<property name="url" value="${jdbc.url}"/>
<!-- 指定连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!-- 指定连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id ="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
</beans>
6.程序主入口:
ApplicationContext ctx = new ClassPathXmlApplicationContext("springMvc.xml");
7.编写service层查询数据库:
@Service
public class TestService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<UserTable> findByName(String name){
String sql = "select * from userTable where name= '"+name+"'";
RowMapper<UserTable> rm = BeanPropertyRowMapper.newInstance(UserTable.class);
List<UserTable> list = jdbcTemplate.query(sql, rm);
return list;
}
}
8.controller层:
@RestController
public class HelloWeb {
@Autowired
private TestService testService;
@RequestMapping("/findByName/{name}")
public Object testJdbc(@PathVariable String name) {
return testService.findByIp(name);
}
}
9.ok部署到tomcat启动项目即可访问接口
gitee源码地址:https://gitee.com/yu_zhe/mySpringMvc