Java搭建springMvc web服务并整合mysql数据库

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼大虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值