导入jar包导入数据库


编写配置文件

Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
<properties resource=""></properties>
<!-- 别名 -->
<typeAliases>
<package name="cn.lexed.pojo"/>
</typeAliases>
</configuration>
db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test2
username=root
password=root编写Student类

Student.java
package cn.lexed.pojo;
public class Student {
	private Integer id;
	private String name;
	private Integer age;
	public Student() {
		super();
	}
	public Student(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = 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;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
}
编写StudentService和StudentServiceImpl

StudentService.java
package cn.lexed.service;
import java.util.List;
import cn.lexed.pojo.Student;
public interface StudentService {
	
	List<Student> selectStudents();//查询所有
	
	Student selectById(int id);
	
}
StudentServiceImpl.java
package cn.lexed.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.lexed.dao.StudentMapper;
import cn.lexed.pojo.Student;
import cn.lexed.service.StudentService;
@Service("studentService")
public class StudentServiceImpl implements StudentService{
	
	@Resource(name="studentMapper")
	private StudentMapper stu;
	
	@Override
	public List<Student> selectStudents() {
		// TODO Auto-generated method stub
		return stu.selectStudents();//调用dao层
	}
	@Override
	public Student selectById(int id) {
		// TODO Auto-generated method stub
		return stu.selectById(id);
	}
	
}
编写StudentMapper

StudentMapper.java
package cn.lexed.dao;
import java.util.List;
import cn.lexed.pojo.Student;
public interface StudentMapper {
	
	List<Student> selectStudents();//查询所有
	
	Student selectById(int id);//根据id查询
}
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="cn.lexed.dao.StudentMapper">
<!-- 查询所有数据 -->
<select id="selectStudents" resultType="Student">
select * from student
</select>
<!-- 根据id查询数据 -->
<select id="selectById" resultType="Student" parameterType="int">
select * from student where id=#{id}
</select>
</mapper>
编写配置文件(点击Namespaces添加context)

app.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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 引入db.properties -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="db.properties"></property>
</bean>
<!-- 配置druid数据库连接池 -->
<bean id="MyDataSouce" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 声明mybatis中提供的SqlSessionFactoryBean -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--  -->
<property name="dataSource" ref="MyDataSouce"></property>
<!-- configLocation:读取配置文件 -->
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
<!-- 创建Dao对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- MapperScannerConfigurer会扫描这个包中所有接口,把每个接口都执行
     一次getMapper()方法,得到每个接口的dao对象,把这个对象放到Spring
     容器中,dao对象的默认名称是 接口名首字母小写 -->
<property name="basePackage" value="cn.lexed.dao"></property>
</bean>
<!-- 配置组件扫描器,扫描类中的注解 -->
<context:component-scan base-package="cn.lexed.service"></context:component-scan>
</beans>
编写测试类

Test.java
package cn.lexed.test;
import static org.junit.Assert.*;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.Student;
import cn.lexed.service.StudentService;
public class Test {
	@org.junit.Test
	public void test() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		StudentService s=(StudentService)ac.getBean("studentService");
		List<Student> stu=s.selectStudents();
		System.out.println(stu);
	}
	@org.junit.Test
	public void test1() {
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		StudentService s=(StudentService)ac.getBean("studentService");
		Student stu=s.selectById(1);
		System.out.println(stu);
	}
}










