Spring
----------
业务层框架。
管理bean的。
new
Map
体验spring
------------
1.创建模块 ,添加pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2.创建java类
public class WelcomeService {
private String message ;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void sayHello(){
System.out.println(message);
}
}
3.创建配置文件
[resources/beans.xml]
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
4.创建App
[App.java]
public static void main(String[] args) {
//创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
WelcomeService ws = (WelcomeService)ac.getBean("ws");
ws.sayHello();
}
spring的注解方式使用
------------------
0.增加pom.xml文件
1.UserDao增加@Repository注解.
@Repository("userDao")
public class UserDao{
...
}
2.Service增加 @Service注解。
@Service("ws")
public class WelcomeService {
...
//注入指定的dao对象
@Resource(name = "userDao")
public void setDao(UserDao dao) {
this.dao = dao;
}
}
3.修改beans.xml文件,引入context空间,使用组件扫描。
[resrouces/beans.xml]
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-4.3.xsd">
4.测试App.java
public class App {
public static void main(String[] args) {
//创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
WelcomeService ws = (WelcomeService)ac.getBean("ws");
ws.sayHello();
}
}
spring 整合mybatis
---------------------
1.创建模块 pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2.创建包
com.it18zhang.springmybatis.dao
com.it18zhang.springmybatis.service
com.it18zhang.springmybatis.util
3.配置beans.xml
[resources/beans.xml]
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
4.编写单元测试
@Test
public void testConn() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
DataSource ds = (DataSource)ac.getBean("dataSource");
System.out.println(ds.getConnection());
}
5.添加domain类
User
Order
Item
略
6.添加Mapper.xml映射文件
//注意:修改类的别名
resources/UserMapper.xml
resources/OrderMapper.xml
7.添加mybatis-config.xml
[resources/mybatis-config.xml]
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
8.创建Dao接口和实现类.
[BaseDao.java]
package com.it18zhang.springmybatis.dao;
import java.util.List;
/**
*基本Dao接口
*/
public interface BaseDao
public void insert(T t) ;
public void update(T t) ;
public void delete(Integer id) ;
public T selectOne(Integer id) ;
public List
}
[UserDao.java]
package com.it18zhang.springmybatis.dao;
import com.it18zhang.springmybatis.domain.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
/**
*/
@Repository("userDao")
public class UserDao extends SqlSessionDaoSupport implements BaseDao
public void insert(User user) {
getSqlSession().insert("users.insert",user);
}
public void update(User user) {
getSqlSession().update("users.update", user);
}
public void delete(Integer id ) {
getSqlSession().delete("users.delete", id);
}
public User selectOne(Integer id) {
return getSqlSession().selectOne("users.selectOne",id) ;
}
public List
return getSqlSession().selectList("users.selectAll");
}
}
[OrderDao.java]
略
9.创建BaseService
[BaseService.java]
package com.it18zhang.springmybatis.service;
import java.util.List;
/**
* Created by Administrator on 2017/4/7.
*/
public interface BaseService
public void insert(T t);
public void update(T t);
public void delete(Integer id);
public T selectOne(Integer id);
public List
}
[BaseServiceImpl.java]
public abstract class BaseServiceImpl
private BaseDao
public void setDao(BaseDao
this.dao = dao;
}
public void insert(T t) {
dao.insert(t);
}
...
}
[UserService.java]
public interface UserService extends BaseService
}
[UserServiceImpl.java]
@Service("userService")
public class UserServiceImpl extends BaseServiceImpl
/*** 重写该方法,注入指定的Dao对象 ***/
@Resource(name="userDao")
public void setDao(BaseDao
super.setDao(dao);
}
}
10.完善spring的配置文件.
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd" default-autowire="byType">
11.测试UserService
@Test
public void testUserService() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
UserService us = (UserService)ac.getBean("userService");
User u = new User();
u.setName("jerry");
u.setAge(12);
us.insert(u);
}
select i.id iid,i.itemname iitemname,o.id oid,o.orderno oorderno , u.id uid ,u.name uname ,u.age uage
from items i
left outer join orders o on i.oid = o.id
left outer join users u on o.uid = u.id
where i.id = 2