spring3+mybatis3+mysql

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

实体类:

复制代码
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;
    }
}
复制代码

mapper:

复制代码
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);
}
复制代码

dao:

复制代码
package org.my.dao;

import org.my.entity.User;

public interface UserDao {
    public User getUserById(User user);
}
复制代码

impl(dao实现):

复制代码
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());
    }

}

复制代码

test测试类:

复制代码
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());
    }
    
}
复制代码

 

beans.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-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>
复制代码

jdbc.proerties:

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

 

接下来是项目截图:

jar包:

 

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

 

 

posted @ 2014-06-05 16:30  mr.g.  阅读(262)  评论(0编辑  收藏  举报