码迷,mamicode.com
首页 > 数据库 > 详细

spring3+mybatis3+mysql

时间:2014-06-07 07:14:07      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   java   a   

玩了一下mybatis,网上源码说的不太清楚。自己写了一下。希望对大家有用(spring3+mybatis3+mysql)

实体类:

bubuko.com,布布扣
bubuko.com,布布扣
package org.my.entity;

public class User {
    private Integer id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}
bubuko.com,布布扣
bubuko.com,布布扣

mapper:

bubuko.com,布布扣
bubuko.com,布布扣
package org.my.maper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.my.entity.User;

public interface UserMapper {
    public void insertUser(User user);
    @Select("SELECT * FROM user WHERE id = #{id}") 
    public User getUser(@Param("id")Integer id);
}
bubuko.com,布布扣
bubuko.com,布布扣

dao:

bubuko.com,布布扣
bubuko.com,布布扣
package org.my.dao;

import org.my.entity.User;

public interface UserDao {
    public User getUserById(User user);
}
bubuko.com,布布扣
bubuko.com,布布扣

impl(dao实现):

bubuko.com,布布扣
bubuko.com,布布扣
package org.my.impl;

import org.my.dao.UserDao;
import org.my.entity.User;
import org.my.maper.UserMapper;

public class UserDaoImpl implements UserDao {
    private UserMapper userMapper;
    public void setUserMapper(UserMapper userMapper) { 
        this.userMapper = userMapper; 
    } 
    @Override
    public User getUserById(User user) {
        return userMapper.getUser(user.getId());
    }

}

bubuko.com,布布扣
bubuko.com,布布扣

test测试类:

bubuko.com,布布扣
bubuko.com,布布扣
package org.my;

import org.my.dao.UserDao;
import org.my.entity.User;
import org.my.impl.UserDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
        "beans.xml"); 
        UserDao userDao = (UserDaoImpl)ctx.getBean("userDaoImpl");
        User user = new User();
        user.setId(1);
        User u = userDao.getUserById(user);
        System.out.println(u.getName());
    }
    
}
bubuko.com,布布扣
bubuko.com,布布扣

 

beans.xml:

bubuko.com,布布扣
bubuko.com,布布扣
<?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-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
                    ">

    <context:property-placeholder location="jdbc.properties" />
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="org.my.maper.UserMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="userDaoImpl" class="org.my.impl.UserDaoImpl">
        <property name="userMapper" ref="userMapper" />
    </bean>


</beans>
bubuko.com,布布扣
bubuko.com,布布扣

jdbc.proerties:

bubuko.com,布布扣
driver=com.mysql.jdbc.Driver
username=root
password=root
url=jdbc\:mysql\://localhost\:3306/test
  
bubuko.com,布布扣

 

接下来是项目截图:

bubuko.com,布布扣

jar包:

 

bubuko.com,布布扣

希望大家按照这个配置,能成功配置好

 

 

spring3+mybatis3+mysql,布布扣,bubuko.com

spring3+mybatis3+mysql

标签:c   class   blog   code   java   a   

原文地址:http://www.cnblogs.com/mrgong/p/3770621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!