spring-mybatis整合

来源:互联网 发布:阿里云数据库 ads 编辑:程序博客网 时间:2024/06/16 11:40

spring-mybatis整合: 实现对学生信息的增删改查。

ant-1.9.6.jar
ant-launcher-1.9.6.jar
asm-5.1.jar
cglib-3.2.4.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
javassist-3.21.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mybatis-3.4.2.jar
mybatis-spring-1.3.1.jar
mysql-connector-java-5.1.7-bin.jar
ognl-3.1.12.jar
slf4j-api-1.7.22.jar
slf4j-log4j12-1.7.22.jar
spring-aop-5.0.1.RELEASE.jar
spring-aspects-5.0.1.RELEASE.jar
spring-beans-5.0.1.RELEASE.jar               
spring-context-5.0.1.RELEASE.jar
spring-core-5.0.1.RELEASE.jar
spring-expression-5.0.1.RELEASE.jar
spring-jcl-5.0.1.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-test-5.0.1.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar

(我这里jar包版本不一样,连接池用的是dbcp, 最热门的现在是Druid)

数据库 test.sql

1.定义学生类

package com.abc.beans;


public class Student {


private Integer id;
private String name;
private int age;


public Student() {
super();
// TODO Auto-generated constructor stub
}


public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}


public void setId(Integer id) {
this.id = id;
}


public void setName(String name) {
this.name = name;
}


public void setAge(int age) {
this.age = age;


}


public Integer getId() {
return id;
}


public String getName() {
return name;
}


public int getAge() {
return age;
}


@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}


}

2.定义service以及实现类   引入dao层

package com.abc.service;


import java.util.List;


import com.abc.beans.Student;


public interface IStudentService {
void saveStudent(Student student);
void removeStudentById(int id);
void modifyStudent(Student student);
 


Student findStudentById(int id) ;
List<Student> findAllStudents();
}

package com.abc.service;


import java.util.List;


import com.abc.beans.Student;
import com.abc.dao.IStudentDao;


public class StduentsImpl implements IStudentService {


private IStudentDao dao;


public void setDao(IStudentDao dao) {
this.dao = dao;
}


public void saveStudent(Student student) {
dao.insertStudent(student);


}


public void removeStudentById(int id) {
dao.deleteStudentById(id);


}


public void modifyStudent(Student student) {
dao.updateStudent(student);


}


public Student findStudentById(int id) {
return dao.selectStudentById(id);
}


public List<Student> findAllStudents() {
return dao.selectAllStudents();


}


}

3.dao层

package com.abc.dao;


import java.util.List;


import com.abc.beans.Student;


public interface IStudentDao {


void insertStudent(Student student);
void deleteStudentById(int id);
void updateStudent(Student student);
 

 
Student selectStudentById(int id) ;
List<Student> selectAllStudents();

}

dao层实现 mapper

<?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.abc.dao.IStudentDao">
<insert id="insertStudent">
   insert into student(name,age) values(#{name},#{age})
</insert>


<delete id="deleteStudentById">                                                sql语句写法,查询的是student对象,要加入resultType;
    delete from student where id =#{xxx}
</delete>
<update id="updateStudent">
      update student set name=#{name},age=#{age}  where id =#{id}
</update>
<select id="selectStudentById" resultType="Student">
     select id,name,age from student  where id =#{xxx}
</select>
<select id="selectAllStudents"  resultType="Student">                    
     select id,name,age from student  
</select>
</mapper>


4.配置spring容器

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop.xsd">


<!-- 注册DataSource数据源四大 -->
<!-- <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
name="url" value="jdbc:mysql:///test" /> <property name="username" value="root" 
/> <property name="password" value="111" /> </bean> -->




<!-- 注册DataSource数据源:DBCP -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql:///test" />
<property name="username" value="root" />
<property name="password" value="111" />
</bean>
<!-- 注册dao层 <bean id="studentDao" class="com.abc.dao.StudentDaoImpl"> <property 
name="dataSource" ref="myDataSource" /> </bean> -->


<bean id="mysqlSessinFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="mybatis.xml" />
</bean>


<!-- 生成Dao --><!-- 扫描Dao -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mysqlSessinFactory" />
<property name="basePackage" value="com.abc.dao" />
</bean>


<bean id="studentService" class="com.abc.service.StduentsImpl">
<property name="dao" ref="IStudentDao" />


</bean>
<!-- 注册平台事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource" />


</bean>


<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="remove" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="modify" isolation="DEFAULT" propagation="REQUIRED" />
<tx:method name="find" isolation="DEFAULT" propagation="REQUIRED"
read-only="true" />


</tx:attributes>
</tx:advice>
<!-- 事务通知 -->
<aop:config>
<aop:pointcut expression="execution(* *..service.*.*(..))"
id="myPointcut" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" />
</aop:config>
</beans>

5.配置mybatis文件 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>


<typeAliases>
   <package name="com.abc.beans"/>
</typeAliases>
<mappers>                                                         mybatis配置beans学生类,以及mapper映射。

                                                                             注意 这里都是<package  name=>
   <package name="com.abc.dao"/>
</mappers>

</configuration>

6.test类测试
package com.abc.test;


import java.util.List;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.abc.beans.Student;
import com.abc.service.IStudentService;


public class Mytest {


private IStudentService service;


@Before
public void before() {
String config = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
service = (IStudentService) ac.getBean("studentService");
}


@Test
public void test01() {


Student student = new Student("ppp1", 23);
service.saveStudent(student);
}


@Test
public void test02() {
service.removeStudentById(2);
}


@Test
public void test03() {
Student student = new Student("平米数", 11);
student.setId(1);
service.modifyStudent(student);
}



public void test06() {
Student student = service.findStudentById(3);
System.out.println(student);
}


@Test
public void test07() {
List<Student> students = service.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
}
}





总结:改动不大,增删改查用得是mybatis实现。