首页 > 代码库 > Spring+Mybatis J2SE小例总结
Spring+Mybatis J2SE小例总结
package dao;import java.util.List;import model.User;public interface UserDao { public User getUser(int i); public List<User> getAllUser(); public int insertUser(User u); public int updateUser(User u); public int deleteUser(int i); }
<?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="dao.UserDao"> <select id="getUser" parameterType="int" resultType="User"> select * from t_user where id=#{id} </select> <select id="getAllUser" resultType="User"> select * from t_user </select> <delete id="deleteUser" parameterType="int" > delete from t_user where id=#{id} </delete> <update id="updateUser" parameterType="User"> update t_user set username=#{username}, password=#{password} where id=#{id} </update> <insert id="insertUser" parameterType="User"> insert into t_user(username,password) values(#{username},#{password}) </insert> </mapper>
package model;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component@Scope("prototype")public class User { private int id; private String username; private String password; public User() { super(); } public User(String username, String password) { super(); this.username = username; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package service;import java.util.List;import model.User;public interface IUserSrv { public User getUser(int i); public List<User> getAllUser(); public int insertUser(User u); public int updateUser(User u); public int deleteUser(int i);}
package service;import java.util.List;import javax.annotation.Resource;import org.springframework.stereotype.Component;import org.springframework.transaction.annotation.Transactional;import dao.UserDao;import model.User;@Component@Transactionalpublic class UserSrv implements IUserSrv {@Resourceprivate UserDao userDao; @Override public User getUser(int i) { // TODO 自动生成的方法存根 return userDao.getUser(i); } @Override public List<User> getAllUser() { // TODO 自动生成的方法存根 return userDao.getAllUser(); } @Override public int insertUser(User u) { // TODO 自动生成的方法存根 return userDao.insertUser(u); } @Override public int updateUser(User u) { // TODO 自动生成的方法存根 return userDao.updateUser(u); } @Override public int deleteUser(int i) { // TODO 自动生成的方法存根 return userDao.deleteUser(i); }}
package test;import javax.annotation.Resource;import org.springframework.stereotype.Component;import model.User;import service.IUserSrv;import util.BeanHelper;@Componentpublic class Test { @Resource private User user; public static void main(String[] args) { IUserSrv userSrv = (IUserSrv) BeanHelper.getBean("userSrv"); System.out.println(userSrv.getUser(1).getUsername()); Test t = (Test) BeanHelper.getBean("test"); t.user.setUsername("dd"); t.user.setPassword("xxx"); System.out.println(userSrv.insertUser(t.user)); }}
package util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BeanHelper { private BeanHelper(){} private static ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); public static Object getBean(String beanName){ return ac.getBean(beanName); }}
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 1. 数据源 : DriverManagerDataSource --> <context:annotation-config></context:annotation-config> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />>Spring+Mybatis J2SE小例总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。