接口:
public interface UserMapper {public List selectAllUser();public User findUserById(int id);public User findUserByNameAndPSWD(Map map);public void addUser(Map map);}资源类(注意这里的属性名和列名不一致):
@Datapublic class User {int id;String name;String pswd;@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", pswd='" + pswd + '\'' +'}';}}这里因为我写了lombok否则是需要书写set方法的!
步骤四: 书写接口的配置文件 select * from justplay.springtestselect * from justplay.springtest where `id`=#{id}select * from justplay.springtest where `name`=#{name} and `password`=#{pswd}insert into `springtest`(`name`,`password`) values (#{name},#{pswd})我们要解决属性名和接口不一致的问题所以我们要定义resultMap
步骤五:书写Spring和Mybatis的配置文件Mybatis:
由于Spring和Mybatis整合了,所以我们的Mybatis配置文件基本上是空的… 之定义了一个类的别名
Spring:
附加:实现log服务的拓展运用Spring的AOP接口我们可以很方便的通过代理模式实现这一点,详细情况看Spring学习日记.
public class AfterLog implements AfterReturningAdvice {@Overridepublic void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println(method.getName() + "方法已完成");}}最后是窗体的实现和测试代码: 登录窗体:
public class LogIn extends JFrame {private ApplicationContext context;private UserMapper userMapper;private TextField textField1=null;private JPasswordField passwordField=null;public void init(){context=new ClassPathXmlApplicationContext("Mybatis-Spring.xml");userMapper=context.getBean("userMapper",UserMapper.class);setTitle("用户登录");setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container container=getContentPane();container.setLayout(new GridLayout(3,2));setVisible(true);JLabel label1=new JLabel("用户名:");JLabel label2=new JLabel("密码:");textField1=new TextField();textField1.addFocusListener(new Commend(textField1,"请输入用户名"));passwordField=new JPasswordField();JButton sure=new JButton("确定");JButton signin=new JButton("注册");sure.addActionListener(new Click(this));signin.addActionListener(new Click(this));container.add(label1);container.add(textField1);container.add(label2);container.add(passwordField);container.add(signin);container.add(sure);pack();}private class Commend implements FocusListener {String mess=null;TextField tf=null;public Commend(TextField textField,String message){mess=message;tf=textField;}@Overridepublic void focusGained(FocusEvent e) {if(tf.getText().equals(mess)){tf.setText("");tf.setForeground(Color.black);}}@Overridepublic void focusLost(FocusEvent e) {if(tf.getText().equals("")){tf.setText(mess);tf.setForeground(Color.gray);}}}private class Click implements ActionListener {private JFrame Frame;public Click(JFrame frame){Frame=frame;}@Overridepublic void actionPerformed(ActionEvent e) {JButton button=(JButton) e.getSource();if(button.getText().equals("确定")){/** * 1.查询数据库 * 2.判断信息 * 3.正确的话进入主界面 * 4.否则重新输入 */Map map=new HashMap();map.put("name",textField1.getText());map.put("pswd",new String(passwordField.getPassword()));User user = userMapper.findUserByNameAndPSWD(map);if(user!=null){//找到了JOptionPane.showMessageDialog(null, "登录成功");Frame.setVisible(false); } else { //没找到 JOptionPane.showMessageDialog(null, "用户名或密码错误"); textField1.setText(""); passwordField.setText(""); }}else {new SignUp().init();}}}}注册窗体:
package com.zhang.UI;import com.zhang.mapper.UserMapper;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;public class SignUp extends JFrame {private ApplicationContext context;private UserMapper userMapper;private TextField textField1=null;private JPasswordField passwordField=null;public void init(){context = new ClassPathXmlApplicationContext("Mybatis-Spring.xml");userMapper = context.getBean("userMapper", UserMapper.class);setTitle("新用户注册");setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);Container container=getContentPane();container.setLayout(new GridLayout(3,2));setVisible(true);JLabel label1=new JLabel("新用户姓名:");JLabel label2=new JLabel("设置密码:");textField1=new TextField();textField1.addFocusListener(new Commend(textField1,"请输入用户名"));passwordField=new JPasswordField();JButton concel=new JButton("取消");JButton sure=new JButton("确定");concel.addActionListener(new Click(this));sure.addActionListener(new Click(this));container.add(label1);container.add(textField1);container.add(label2);container.add(passwordField);container.add(concel);container.add(sure);pack();}private class Commend implements FocusListener{String mess=null;TextField tf=null;public Commend(TextField textField,String message){mess=message;tf=textField;}@Overridepublic void focusGained(FocusEvent e) {if(tf.getText().equals(mess)){tf.setText("");tf.setForeground(Color.black);}}@Overridepublic void focusLost(FocusEvent e) {if(tf.getText().equals("")){tf.setText(mess);tf.setForeground(Color.gray);}}}private class Click implements ActionListener{private JFrame Frame;public Click(JFrame frame){Frame=frame;}@Overridepublic void actionPerformed(ActionEvent e) {JButton button=(JButton) e.getSource();if(button.getText().equals("取消")){Frame.setVisible(false);}else {HashMap map = new HashMap();map.put("name",textField1.getText());map.put("pswd",new String(passwordField.getPassword()));userMapper.addUser(map);Frame.setVisible(false);}}}}测试代码:
import com.zhang.UI.LogIn;import com.zhang.UI.SignUp;import com.zhang.mapper.UserMapper;import com.zhang.pojo.User;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.HashMap;import java.util.Map;public class MyTest {@Testpublic void test1(){ApplicationContext context = new ClassPathXmlApplicationContext("Mybatis-Spring.xml");UserMapper userMapper = context.getBean("userMapper", UserMapper.class);Map map=new HashMap();//map.put("name","zhangSan");//map.put("pswd","1234");//userMapper.addUser(map);for (User user : userMapper.selectAllUser()) {System.out.println(user);}}@Testpublic void signInTest(){new SignUp().init();}public static void main(String[] args) {new LogIn().init();}}