首页 > 代码库 > spring3+mybatis3+mysql

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包:

 

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