seam框架CRUD
方权
2023-12-01
首先,配置数据源context.xml 将这个文件放在<catalina_home>的conf目录下,我用的是tomcat6
context.xml 内容如下:
Xml代码
1.<Resource name="jdbc/BBS" auth="Container"
2. type="javax.sql.DataSource"
3. driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
4. url="jdbc:sqlserver://192.168.1.1:1433;DatabaseName=infodb1"
5. username="sa" password="sa" maxActive="4" maxIdle="2"
6. maxWait="5000" />
<Resource name="jdbc/BBS" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://192.168.1.1:1433;DatabaseName=infodb1"
username="sa" password="sa" maxActive="4" maxIdle="2"
maxWait="5000" />
在这里,我用的数据库是sql server2005 ,如果改为mysql数据库的话,需要修改driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
换成mysql的数据库驱动。
配置seam 的components.xml,该文件放在WEB-INF目录下。
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<components xmlns="http://jboss.com/products/seam/components"
3. xmlns:core="http://jboss.com/products/seam/core"
4. xmlns:persistence="http://jboss.com/products/seam/persistence"
5. xmlns:security="http://jboss.com/products/seam/security"
6. xmlns:transaction="http://jboss.com/products/seam/transaction"
7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
8. xsi:schemaLocation="http://jboss.com/products/seam/core
9. http://jboss.com/products/seam/core-2.1.xsd http://jboss.com/products/seam/persistence
10. http://jboss.com/products/seam/persistence-2.1.xsd http://jboss.com/products/seam/transaction
11. http://jboss.com/products/seam/transaction-2.1.xsd http://jboss.com/products/seam/security
12. http://jboss.com/products/seam/security-2.1.xsd http://jboss.com/products/seam/components
13. http://jboss.com/products/seam/components-2.1.xsd
14. http://jboss.com/products/seam/spring
15. http://jboss.com/products/seam/spring-2.1.xsd">
16. <core:init debug="false" />
17. <core:manager concurrent-request-timeout="500"
18. conversation-id-parameter="cid" conversation-timeout="120000" />
19. <transaction:entity-transaction entity-manager="#{em}" />
20. <persistence:entity-manager-factory name="BBS" />
21. <persistence:managed-persistence-context auto-create="true"
22. entity-manager-factory="#{BBS}" name="em" />
23.
24. <security:identity
25. authenticate-method="#{authenticator.authenticate}" />
26. <core:pojo-cache
27. cfg-resource-name="#{facesContext.externalContext.context.getRealPath('/WEB-INF/cache.xml')}" />
28.</components>
29.
30.
31.接着,我们写一个dao的接口
<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
xmlns:core="http://jboss.com/products/seam/core"
xmlns:persistence="http://jboss.com/products/seam/persistence"
xmlns:security="http://jboss.com/products/seam/security"
xmlns:transaction="http://jboss.com/products/seam/transaction"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.com/products/seam/core
http://jboss.com/products/seam/core-2.1.xsd http://jboss.com/products/seam/persistence
http://jboss.com/products/seam/persistence-2.1.xsd http://jboss.com/products/seam/transaction
http://jboss.com/products/seam/transaction-2.1.xsd http://jboss.com/products/seam/security
http://jboss.com/products/seam/security-2.1.xsd http://jboss.com/products/seam/components
http://jboss.com/products/seam/components-2.1.xsd
http://jboss.com/products/seam/spring
http://jboss.com/products/seam/spring-2.1.xsd">
<core:init debug="false" />
<core:manager concurrent-request-timeout="500"
conversation-id-parameter="cid" conversation-timeout="120000" />
<transaction:entity-transaction entity-manager="#{em}" />
<persistence:entity-manager-factory name="BBS" />
<persistence:managed-persistence-context auto-create="true"
entity-manager-factory="#{BBS}" name="em" />
<security:identity
authenticate-method="#{authenticator.authenticate}" />
<core:pojo-cache
cfg-resource-name="#{facesContext.externalContext.context.getRealPath('/WEB-INF/cache.xml')}" />
</components>
接着,我们写一个dao的接口Xml代码
1.
注意 <transaction:entity-transaction entity-manager="#{em}" />
<persistence:entity-manager-factory name="BBS" />
<persistence:managed-persistence-context auto-create="true"
entity-manager-factory="#{BBS}" name="em" /> 这几句,其中 <persistence:entity-manager-factory name="BBS" /> 要跟上面的context.xml中的《Resource name="jdbc/BBS" 》一致。
接着,我们写一个dao的接口
Java代码
1.public interface UserDAO {
2. public void saveUser(User user);
3.}
public interface UserDAO {
public void saveUser(User user);
} Java代码
1.<PRE class=java name="code">@Name(" UserDAO ")
2.@AutoCreate
3.
4.public class UserDAOImpl implements UserDAO {
5. @In
6. private EntityManager em;
7. public void saveUser(User user) throws PersistenceException {
8. em.persist(user);
9. }
10.}
11.</PRE>
12.
Java代码 @Name(" UserDAO ") @AutoCreate public class UserDAOImpl implements UserDAO { @In private EntityManager em; public void saveUser(User user) throws PersistenceException { em.persist(user); } } @Name(" UserDAO ")
@AutoCreate
public class UserDAOImpl implements UserDAO {
@In
private EntityManager em;
public void saveUser(User user) throws PersistenceException {
em.persist(user);
}
}
Java代码
1.
@In
private EntityManager em; 之际就注入进来了,不需要在写数数据库连接的东西了。
在faces层:
@In
private UserDAO userDAO;
这样就可以引入DAO进行操作了