Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.01 KB | None | 0 0
  1. package com.gif.code.controller;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. import com.gif.code.model.GifModel;
  13. import com.gif.code.service.GifService;
  14.  
  15. @RestController
  16. public class GifController {
  17.  
  18. @Autowired
  19. GifService gifService;
  20.  
  21. @RequestMapping(value = "/getAllData", method = RequestMethod.GET, headers = "Accept=application/json")
  22. public List<GifModel> getData() {
  23. System.out.println("In Controller");
  24. List<GifModel> listOfData = gifService.getAllData();
  25. return listOfData;
  26. }
  27.  
  28. @RequestMapping(value = "/getData/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
  29. public GifModel getDataById(@PathVariable int id) {
  30. return gifService.getData(id);
  31. }
  32.  
  33. @RequestMapping(value = "/addData", method = RequestMethod.POST, headers = "Accept=application/json")
  34. public void addData(@RequestBody GifModel gifModel) {
  35. gifService.addData(gifModel);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. package com.gif.code.model;
  42.  
  43. import javax.persistence.Column;
  44. import javax.persistence.Entity;
  45. import javax.persistence.GeneratedValue;
  46. import javax.persistence.GenerationType;
  47. import javax.persistence.Id;
  48. import javax.persistence.Table;
  49.  
  50. @Entity
  51. @Table(name="WEBSITE")
  52. public class GifModel {
  53.  
  54. @Id
  55. @Column(name="id")
  56. @GeneratedValue(strategy=GenerationType.IDENTITY)
  57. int id;
  58.  
  59. @Column(name="title")
  60. String title;
  61.  
  62. @Column(name="description")
  63. String description;
  64.  
  65. public GifModel() {
  66. super();
  67. }
  68. public GifModel(int i, String title,String description) {
  69. super();
  70. this.id = i;
  71. this.title = title;
  72. this.description=description;
  73. }
  74. public int getId() {
  75. return id;
  76. }
  77. public void setId(int id) {
  78. this.id = id;
  79. }
  80. public String getTitle() {
  81. return title;
  82. }
  83. public void setTitle(String title) {
  84. this.title = title;
  85. }
  86. public String getDescription() {
  87. return description;
  88. }
  89. public void setDescription(String description) {
  90. this.description = description;
  91. }
  92.  
  93. package com.gif.code.service;
  94.  
  95.  
  96. import java.util.List;
  97.  
  98. import org.springframework.beans.factory.annotation.Autowired;
  99. import org.springframework.stereotype.Service;
  100. import org.springframework.transaction.annotation.Transactional;
  101.  
  102. import com.gif.code.dao.GifDao;
  103. import com.gif.code.model.GifModel;
  104.  
  105. @Service("gifService")
  106. public class GifService {
  107.  
  108. @Autowired
  109. GifDao gifDao;
  110.  
  111. @Transactional
  112. public List<GifModel> getAllData() {
  113. System.out.println("In Service");
  114. return gifDao.getAllData();
  115. }
  116.  
  117. @Transactional
  118. public GifModel getData(int id) {
  119. return gifDao.getData(id);
  120. }
  121.  
  122. @Transactional
  123. public void addData(GifModel gifmodel) {
  124. gifDao.addData(gifmodel);
  125. }
  126.  
  127. }
  128.  
  129. package com.gif.code.dao;
  130.  
  131. import java.util.List;
  132.  
  133. import org.hibernate.Session;
  134. import org.hibernate.SessionFactory;
  135. import org.springframework.beans.factory.annotation.Autowired;
  136. import org.springframework.stereotype.Repository;
  137.  
  138. import com.gif.code.model.GifModel;
  139.  
  140. @Repository
  141. public class GifDao {
  142.  
  143. @Autowired
  144. private SessionFactory sessionFactory;
  145.  
  146. public void setSessionFactory(SessionFactory sf) {
  147. this.sessionFactory = sf;
  148. }
  149.  
  150. @SuppressWarnings("unchecked")
  151. public List<GifModel> getAllData() {
  152. System.out.println("In Dao");
  153. Session session = this.sessionFactory.getCurrentSession();
  154. List<GifModel> textList = session.createQuery("from GifModel").list();
  155. return textList;
  156. }
  157.  
  158. public GifModel getData(int id) {
  159. Session session = this.sessionFactory.getCurrentSession();
  160. GifModel gifmodel = (GifModel) session.load(GifModel.class, new Integer(id));
  161. return gifmodel;
  162. }
  163.  
  164. public GifModel addData(GifModel gifmodel) {
  165. Session session = this.sessionFactory.getCurrentSession();
  166. session.persist(gifmodel);
  167. return gifmodel;
  168. }
  169.  
  170. }
  171.  
  172. <?xml version="1.0" encoding="UTF-8"?>
  173. <beans:beans xmlns="http://www.springframework.org/schema/mvc"
  174. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
  175. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  176. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  177. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  178. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  179. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  180.  
  181. <annotation-driven />
  182.  
  183. <resources mapping="/resources/**" location="/resources/" />
  184.  
  185. <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  186. destroy-method="close">
  187. <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
  188. <beans:property name="url"
  189. value="jdbc:mysql://localhost:3306/gifdata" />
  190. <beans:property name="username" value="root" />
  191. <beans:property name="password" value="Chirpn123" />
  192. </beans:bean>
  193.  
  194. <!-- Hibernate 4 SessionFactory Bean definition -->
  195. <beans:bean id="hibernate4AnnotatedSessionFactory"
  196. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  197. <beans:property name="dataSource" ref="dataSource" />
  198. <beans:property name="annotatedClasses">
  199. <beans:list>
  200. <beans:value>com.gif.code.model.GifModel</beans:value>
  201. </beans:list>
  202. </beans:property>
  203. <beans:property name="hibernateProperties">
  204. <beans:props>
  205. <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
  206. </beans:prop>
  207. <beans:prop key="hibernate.show_sql">true</beans:prop>
  208. </beans:props>
  209. </beans:property>
  210. </beans:bean>
  211.  
  212. <context:component-scan base-package="com.gif.code" />
  213.  
  214. <tx:annotation-driven transaction-manager="transactionManager"/>
  215.  
  216. <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  217. <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
  218. </beans:bean>
  219.  
  220.  
  221. </beans:beans>
  222.  
  223. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  224. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  225. <modelVersion>4.0.0</modelVersion>
  226. <groupId>com.gif.code</groupId>
  227. <artifactId>Gif</artifactId>
  228. <packaging>war</packaging>
  229. <version>0.0.1-SNAPSHOT</version>
  230. <name>Gif Maven Webapp</name>
  231. <url>http://maven.apache.org</url>
  232. <dependencies>
  233. <dependency>
  234. <groupId>junit</groupId>
  235. <artifactId>junit</artifactId>
  236. <version>3.8.1</version>
  237. <scope>test</scope>
  238. </dependency>
  239. <!-- <dependency>
  240. <groupId>org.springframework</groupId>
  241. <artifactId>spring-context</artifactId>
  242. <version>3.1.0.RELEASE</version>
  243. </dependency> -->
  244. <dependency>
  245. <groupId>javax.servlet</groupId>
  246. <artifactId>javax.servlet-api</artifactId>
  247. <version>3.1.0</version>
  248. </dependency>
  249.  
  250. <dependency>
  251. <groupId>org.springframework</groupId>
  252. <artifactId>spring-core</artifactId>
  253. <version>${spring.version}</version>
  254. </dependency>
  255. <dependency>
  256. <groupId>org.springframework</groupId>
  257. <artifactId>spring-webmvc</artifactId>
  258. <version>${spring.version}</version>
  259. </dependency>
  260. <dependency>
  261. <groupId>com.fasterxml.jackson.core</groupId>
  262. <artifactId>jackson-databind</artifactId>
  263. <version>2.4.1</version>
  264. </dependency>
  265. <!-- Hibernate -->
  266. <dependency>
  267. <groupId>org.hibernate</groupId>
  268. <artifactId>hibernate-core</artifactId>
  269. <version>${hibernate.version}</version>
  270. </dependency>
  271. <dependency>
  272. <groupId>org.hibernate</groupId>
  273. <artifactId>hibernate-entitymanager</artifactId>
  274. <version>${hibernate.version}</version>
  275. </dependency>
  276.  
  277. <!-- Apache Commons DBCP -->
  278. <dependency>
  279. <groupId>commons-dbcp</groupId>
  280. <artifactId>commons-dbcp</artifactId>
  281. <version>1.4</version>
  282. </dependency>
  283. <!-- Spring ORM -->
  284. <dependency>
  285. <groupId>org.springframework</groupId>
  286. <artifactId>spring-orm</artifactId>
  287. <version>${spring.version}</version>
  288. </dependency>
  289.  
  290. <!-- AspectJ -->
  291. <dependency>
  292. <groupId>org.aspectj</groupId>
  293. <artifactId>aspectjrt</artifactId>
  294. <version>${org.aspectj-version}</version>
  295. </dependency>
  296. <dependency>
  297. <groupId>mysql</groupId>
  298. <artifactId>mysql-connector-java</artifactId>
  299. <version>5.1.6</version>
  300. </dependency>
  301. </dependencies>
  302. <build>
  303. <finalName>Gif</finalName>
  304.  
  305. <plugins>
  306. <plugin>
  307. <groupId>org.apache.maven.plugins</groupId>
  308. <artifactId>maven-compiler-plugin</artifactId>
  309. <version>3.1</version>
  310. <configuration>
  311. <source>${jdk.version}</source>
  312. <target>${jdk.version}</target>
  313. </configuration>
  314. </plugin>
  315. <plugin>
  316. <groupId>org.apache.maven.plugins</groupId>
  317. <artifactId>maven-war-plugin</artifactId>
  318. <configuration>
  319. <failOnMissingWebXml>false</failOnMissingWebXml>
  320. </configuration>
  321. </plugin>
  322. </plugins>
  323.  
  324. </build>
  325. <properties>
  326. <spring.version>4.2.1.RELEASE</spring.version>
  327. <security.version>4.0.3.RELEASE</security.version>
  328. <jdk.version>1.7</jdk.version>
  329. <hibernate.version>4.3.5.Final</hibernate.version>
  330. <org.aspectj-version>1.7.4</org.aspectj-version>
  331. </properties>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement