Skip to content

Commit

Permalink
Fix the issues with MyBatis Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkhan committed Apr 10, 2015
1 parent 368b934 commit a85090e
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 62 deletions.
9 changes: 5 additions & 4 deletions src/com/markz/testApp/config/DataConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
Expand All @@ -10,14 +11,14 @@
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

@Configuration
@MapperScan("com.markz.testApp.persistence")
@MapperScan("com.markz.testApp.mappers")
public class DataConfig {

@Bean
public DataSource dataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
String url = "jdbc:mysql://enliteinstance.c9a1yjg7i5cn.us-east-1.rds.amazonaws.com:3306:test";
String url = "jdbc:mysql://enliteinstance.c9a1yjg7i5cn.us-east-1.rds.amazonaws.com:3306/test";
String username = "master";
String password = "enliteUs";
dataSource.setUrl(url);
Expand All @@ -32,12 +33,12 @@ public DataSourceTransactionManager transactionManager() {
}

@Bean
public SqlSessionFactoryBean sqlSessionFactory() {
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.markz.testApp.objects");

return sessionFactory;
return sessionFactory.getObject();
}

}
25 changes: 16 additions & 9 deletions src/com/markz/testApp/controller/MyTestController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.markz.testApp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.markz.testApp.mappers.UserDetailsMapper;
import com.markz.testApp.objects.UserDetail;

@RestController
public class MyTestController {

@RequestMapping(value="/users/{user}")
public UserDetail message(@PathVariable String user){
UserDetail userDetail = new UserDetail();
userDetail.setAge(21);
userDetail.setName(user);
userDetail.setUserId(user+"@markz.com");
userDetail.setZip(53717);

return userDetail;
@Autowired
UserDetailsMapper userDetailsMapper;

@RequestMapping(value = "/users/{id}")
public UserDetail getUserById(@PathVariable int id) {
return userDetailsMapper.getUserDetail(id);
}

@RequestMapping(value = "/users")
public List<UserDetail> userList() {
return userDetailsMapper.getAllUsers();
}

}
18 changes: 0 additions & 18 deletions src/com/markz/testApp/dao/UserDetailDao.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/com/markz/testApp/mappers/UserDetailsMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface UserDetailsMapper {
// @Select("SELECT * FROM user_detail where user_id={#userId}")
public UserDetail getUserDetail(int id);

public UserDetail getUserDetailByLoginId(String loginId);

// public int insertUserDetail(UserDetail userDetail);

// public void updateUserDetail(UserDetail userDetail);
Expand Down
37 changes: 37 additions & 0 deletions src/com/markz/testApp/mappers/UserDetailsMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.markz.testApp.mappers.UserDetailsMapper">

<resultMap type="com.markz.testApp.objects.UserDetail" id="UserResult">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="age" column="age" />
<result property="name" column="name" />
<result property="zip" column="zip" />
</resultMap>

<select id="getUserDetail" parameterType="int"
resultType="com.markz.testApp.objects.UserDetail">
SELECT
id as id,
user_id as userId,
age as age,
zip as zip,
name as name
FROM user_details
WHERE id=#{id}
</select>



<select id="getAllUsers" parameterType="string" resultMap="UserResult">
SELECT * FROM user_details
</select>

<select id="getUserDetailByLoginId" resultMap="UserResult">
SELECT * FROM user_details
WHERE user_id=#{loginId}
</select>
</mapper>
29 changes: 0 additions & 29 deletions src/com/markz/testApp/persistence/UserDetailsMapper.xml

This file was deleted.

Binary file modified target/classes/com/markz/testApp/Objects/UserDetail.class
Binary file not shown.
Binary file modified target/classes/com/markz/testApp/config/DataConfig.class
Binary file not shown.
Binary file modified target/classes/com/markz/testApp/controller/MyTestController.class
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions target/classes/com/markz/testApp/mappers/UserDetailsMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.markz.testApp.mappers.UserDetailsMapper">

<resultMap type="com.markz.testApp.objects.UserDetail" id="UserResult">
<id property="id" column="id" />
<result property="userId" column="user_id" />
<result property="age" column="age" />
<result property="name" column="name" />
<result property="zip" column="zip" />
</resultMap>

<select id="getUserDetail" parameterType="int"
resultType="com.markz.testApp.objects.UserDetail">
SELECT
id as id,
user_id as userId,
age as age,
zip as zip,
name as name
FROM user_details
WHERE id=#{id}
</select>



<select id="getAllUsers" parameterType="string" resultMap="UserResult">
SELECT * FROM user_details
</select>

<select id="getUserDetailByLoginId" resultMap="UserResult">
SELECT * FROM user_details
WHERE user_id=#{loginId}
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Mon Apr 06 23:57:30 CDT 2015
#Thu Apr 09 23:15:12 CDT 2015
version=0.0.1-SNAPSHOT
groupId=SpringMVC
m2e.projectName=SpringMVC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
</dependencies>
</project>

0 comments on commit a85090e

Please sign in to comment.