首页 > 代码库 > mybatis-spring整合(学习中)
mybatis-spring整合(学习中)
目录结构:
jar包
spring配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:p="http://www.springframework.org/schema/p" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 9 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">10 11 <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">12 <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/>13 <property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/mybatis"/>14 <property name="username" value="http://www.mamicode.com/root"/>15 <property name="password" value=""/>16 </bean>17 18 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">19 <property name="dataSource" ref="ds"></property>20 <property name="typeAliasesPackage" value="http://www.mamicode.com/com.sm_test.domain"></property>21 <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis-config.xml"></property>22 </bean>23 24 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">25 <property name="basePackage" value="http://www.mamicode.com/com.sm_test.mapper"></property>26 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>27 </bean>28 29 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">30 <property name="mapperInterface" value="http://www.mamicode.com/com.sm_test.mapper.UserMapper"></property>31 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>32 </bean>33 </beans>
mybatis配置文件
1 <?xml version="1.0" encoding="UTF-8"?>2 <!DOCTYPE configuration3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"4 "http://mybatis.org/dtd/mybatis-3-config.dtd">5 <configuration>6 <mappers>7 <mapper resource="com/sm_test/mapper/userMapper.xml"/>8 </mappers>9 </configuration>
实体类
1 package com.sm_test.domain; 2 3 import java.util.Date; 4 5 public class User { 6 7 private int id; 8 private String name; 9 private Date birth;10 private double salary;11 public User(int id, String name, Date birth, double salary) {12 super();13 this.id = id;14 this.name = name;15 this.birth = birth;16 this.salary = salary;17 }18 public User() {19 super();20 }21 public int getId() {22 return id;23 }24 public void setId(int id) {25 this.id = id;26 }27 public String getName() {28 return name;29 }30 public void setName(String name) {31 this.name = name;32 }33 public Date getBirth() {34 return birth;35 }36 public void setBirth(Date birth) {37 this.birth = birth;38 }39 public double getSalary() {40 return salary;41 }42 public void setSalary(double salary) {43 this.salary = salary;44 }45 @Override46 public String toString() {47 return "User [id=" + id + ", name=" + name + ", birth=" + birth48 + ", salary=" + salary + "]";49 }50 51 }
UserMapper.java
1 package com.sm_test.mapper; 2 3 import java.util.List; 4 5 import com.sm_test.domain.User; 6 7 public interface UserMapper { 8 void save(User user); 9 10 void update(User user);11 12 void delete(int id);13 14 User findById(int id);15 16 List<User> findAll();17 }
userMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.sm_test.mapper.UserMapper"> 6 <insert id="save" parameterType="User"> 7 insert into s_user(name,birth,salary) values(#{name},#{birth},#{salary}) 8 </insert> 9 10 <update id="update" parameterType="User">11 update s_user set name=#{name},birth=#{birth},salary=#{salary} where id=#{id}12 </update>13 14 <delete id="delete" parameterType="int">15 delete from s_user where id=#{id}16 </delete>17 18 <select id="findById" parameterType="int" resultType="User">19 select id,name,birth,salary from s_user where id=#{id}20 </select>21 22 <select id="findAll" parameterType="int" resultType="User">23 select id,name,birth,salary from s_user24 </select>25 </mapper>
p.s. userMapper.xml中所有的id应与接口名称一致
测试
1 package com.sm_test.test; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.sm_test.mapper.UserMapper; 7 8 public class SMTest { 9 @SuppressWarnings("resource")10 public static void main(String[] args) {11 // User u=new User(-1,"zz",new Date(),32323); 12 // Resource res = new ClassPathResource("beans.xml");13 // BeanFactory factory = new XmlBeanFactory(res);14 // UserMapper userMapper = (UserMapper)factory.getBean("userMapper");15 // userMapper.save(u);16 17 ///////////////////////////////////////////////////////////////////////////////////////// 18 19 ApplicationContext ctx=null; 20 ctx=new ClassPathXmlApplicationContext("beans.xml"); 21 UserMapper userMapper=(UserMapper)ctx.getBean("userMapper"); 22 23 //插入24 // User u=new User(-1,"yy",new Date(),6757);25 // userMapper.save(u);26 27 //更新28 // User u=new User(1,"zy",new Date("1995/2/21"),10000);29 // userMapper.update(u);30 31 //删除32 // userMapper.delete(2);33 34 //查询35 // System.out.println(userMapper.findById(6));36 37 //查询所有38 System.out.println(userMapper.findAll());39 }40 }
mybatis-spring整合(学习中)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。