Intellij Spring - Jdbc 사용법

2 minute read

Database 사용법

  • 기본적인 JDBC

    • applicationContext.xml.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: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 https://www.springframework.org/schema/context/spring-context.xsd">
                ...
                <context:component-scan base-package="com.ex.crud"/>
      	
                <bean id="connectionMaker" class="com.ex.crud.dao.MConnectionMaker"/>
        </beans>	
      
    • ConnectionMaker.java
        public interface ConnectionMaker {
                Connection makeConnection() throws ClassNotFoundException, SQLException;
        }		
      
    • MConnectionMaker.java
        public class MConnectionMaker implements ConnectionMaker {
                public Connection makeConnection() throws ClassNotFoundException, SQLException {
                        Class.forName("com.mysql.jdbc.Driver");
                        return DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "1234");
                }
        }		
      
    • UserDao.java
        public interface UserDao {
                void insert(User user) throws ClassNotFoundException, SQLException;
                User get(String id) throws SQLException, ClassNotFoundException;
                ...
        }	
      
    • UserDaoJdbc.java
        public class UserDaoJdbc implements UserDao {
                @Autowired
                ConnectionMaker connectionMaker;
      
                ...
      
                public void insert(User user) throws ClassNotFoundException, SQLException {
                        Connection conn = connectionMaker.makeConnection();
                        PreparedStatement pstmt = conn.prepareStatement("insert into curd_user values(?, ?, ?, ?)");
      
                        pstmt.setString(1, user.getId());
                        pstmt.setString(2, user.getPassword());
                        pstmt.setString(3, user.getName());
                        pstmt.setString(4, user.getEmail());
                        pstmt.executeUpdate();
      
                        pstmt.close();
                        conn.close();
                }
      
                public User get(String id) throws SQLException, ClassNotFoundException {
                        Connection conn = connectionMaker.makeConnection();
                        PreparedStatement pstmt = conn.prepareStatement("select * from curd_user where id = ?");
                        pstmt.setString(1, id);
      
                        ResultSet rs = pstmt.executeQuery();
                        rs.next();
                        User user = new User(rs.getString("id"),
                                        rs.getString("password"),
                                        rs.getString("name"),
                                        rs.getString("email"));
      
                        pstmt.close();
                        conn.close();
                        return user;
                }
      				
                ...
        }	
      
  • DataSource 사용

    • applicationContext.xml.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: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 https://www.springframework.org/schema/context/spring-context.xsd">
                ...
                <context:component-scan base-package="com.ex.crud"/>
      	
                <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
                        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                        <property name="url" value="jdbc:mysql://localhost:3306/testDB?useSSL=false"/>
                        <property name="username" value="root"/>
                        <property name="password" value="1234"/>
                </bean>
        </beans>	
      
      UserDao.java
        public interface UserDao {
                void insert(User user) throws ClassNotFoundException, SQLException;
                User get(String id) throws SQLException, ClassNotFoundException;
                ...
        }	
      
    • UserDaoJdbc.java
        public class UserDaoJdbc implements UserDao {
                @Autowired
                DataSource dataSource;
      
                ...
      
                public void insert(User user) throws ClassNotFoundException, SQLException {
                        Connection conn = dataSource.getConnection();
                        PreparedStatement pstmt = conn.prepareStatement("insert into curd_user values(?, ?, ?, ?)");
      
                        pstmt.setString(1, user.getId());
                        pstmt.setString(2, user.getPassword());
                        pstmt.setString(3, user.getName());
                        pstmt.setString(4, user.getEmail());
                        pstmt.executeUpdate();
      
                        pstmt.close();
                        conn.close();
                }
      
                public User get(String id) throws SQLException, ClassNotFoundException {
                        Connection conn = dataSource.getConnection();
                        PreparedStatement pstmt = conn.prepareStatement("select * from curd_user where id = ?");
                        pstmt.setString(1, id);
      
                        ResultSet rs = pstmt.executeQuery();
                        rs.next();
                        User user = new User(rs.getString("id"),
                                        rs.getString("password"),
                                        rs.getString("name"),
                                        rs.getString("email"));
      
                        pstmt.close();
                        conn.close();
                        return user;
                }
      				
                ...
        }	
      
  • JdbcTemplat 사용

    • applicationContext.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: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 https://www.springframework.org/schema/context/spring-context.xsd">
                ...
                <context:component-scan base-package="com.ex.crud"/>
      
                <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
                        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
                        <property name="url" value="jdbc:mysql://localhost:3306/testDB?useSSL=false"/>
                        <property name="username" value="root"/>
                        <property name="password" value="1234"/>
                </bean>
        </beans>	
      
      UserDao.java
        public interface UserDao {
                void insert(User user);
                User get(String id);
                ...
        }	
      
    • UserDaoJdbc.java
        public class UserDaoJdbc implements UserDao {
                public void setDataSource(DataSource dataSource) {
                        this.jdbcTemplate = new JdbcTemplate(dataSource);
                }
      
                private JdbcTemplate jdbcTemplate;
      				
                private RowMapper<User> userMapper = new RowMapper<User>() {
                        public User mapRow(ResultSet resultSet, int i) throws SQLException {
                                return new User(resultSet.getString("id"),
                                                resultSet.getString("password"),
                                                resultSet.getString("name"),
                                                resultSet.getString("email")
                                );
                        }
                };
      
                ...
      				
                public void insert(User user) {
                        jdbcTemplate.update("insert into curd_user values(?, ?, ?, ?)",
                                        user.getId(),
                                        user.getName(),
                                        user.getPassword(),
                                        user.getEmail());
                }
      
                public User get(String id) {
                        return jdbcTemplate.queryForObject("select * form curd_user where id = ?", new Object[]{id}, this.userMapper);
                }
      				
                ...
        }	
      
토비의 스프링 3.1

Categories:

Updated:

Leave a comment